<?php

declare(strict_types=1);

namespace Tvdt\Controller\Backoffice;

use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\HeaderUtils;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Routing\Requirement\Requirement;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Contracts\Translation\TranslatorInterface;
use Tvdt\Controller\AbstractController;
use Tvdt\Entity\Quiz;
use Tvdt\Entity\Season;
use Tvdt\Enum\FlashType;
use Tvdt\Form\CreateSeasonFormType;
use Tvdt\Helpers\FilenameSanitizer;
use Tvdt\Repository\SeasonRepository;
use Tvdt\Security\Voter\SeasonVoter;
use Tvdt\Service\QuizSpreadsheetService;

#[AsController]
#[IsGranted('IS_AUTHENTICATED')]
final class BackofficeController extends AbstractController
{
    public function __construct(
        private readonly SeasonRepository $seasonRepository,
        private readonly Security $security,
        private readonly QuizSpreadsheetService $excel,
        private readonly EntityManagerInterface $em,
        private readonly TranslatorInterface $translator,
    ) {}

    #[Route('/backoffice/', name: 'tvdt_backoffice_index')]
    public function index(): Response
    {
        $seasons = $this->security->isGranted('ROLE_ADMIN')
            ? $this->seasonRepository->findAll()
            : $this->seasonRepository->getSeasonsForUser($this->authenticatedUser);

        return $this->render('backoffice/index.html.twig', [
            'seasons' => $seasons,
        ]);
    }

    #[Route('/backoffice/season/add', name: 'tvdt_backoffice_season_add', priority: 10)]
    public function addSeason(Request $request): Response
    {
        $season = new Season();
        $form = $this->createForm(CreateSeasonFormType::class, $season);

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $season->addOwner($this->authenticatedUser);
            $season->generateSeasonCode();

            $this->em->persist($season);
            $this->em->flush();

            return $this->redirectToRoute('tvdt_backoffice_season', ['seasonCode' => $season->seasonCode]);
        }

        return $this->render('backoffice/season_add.html.twig', ['form' => $form]);
    }

    #[Route('/backoffice/template', name: 'tvdt_backoffice_template', priority: 10)]
    public function getTemplate(): StreamedResponse
    {
        $response = new StreamedResponse($this->excel->generateTemplate());
        $response->headers->set('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        $response->headers->set('Content-Disposition', 'attachment; filename="template.xlsx"');

        return $response;
    }

    #[IsGranted(SeasonVoter::EDIT, subject: 'quiz')]
    #[Route(
        '/backoffice/quiz/{quiz}/export',
        name: 'tvdt_backoffice_quiz_export',
        requirements: ['quiz' => Requirement::UUID],
        methods: ['GET'],
    )]
    public function exportQuiz(Quiz $quiz): Response
    {
        if (!$this->authenticatedUser->isVerified) {
            $this->addFlash(FlashType::Warning, $this->translator->trans('Please confirm your email address before exporting a quiz.'));

            return $this->redirectToRoute('tvdt_backoffice_season', ['seasonCode' => $quiz->season->seasonCode]);
        }

        $response = new StreamedResponse($this->excel->quizToXlsx($quiz));
        $response->headers->set('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        $response->headers->set('Content-Disposition', HeaderUtils::makeDisposition(HeaderUtils::DISPOSITION_ATTACHMENT, FilenameSanitizer::sanitize($quiz->name).'.xlsx'));

        return $response;
    }
}
