<?php

namespace CpCreation\VitiCore\Bridge\Symfony\Bundle\Controller\Admin;

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Form\Admin\SeasonType;
use CpCreation\VitiCore\Season\Model\Season;
use CpCreation\VitiCore\Season\Repository\SeasonRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;

#[Route('/season')]
#[IsGranted(new Expression("is_granted('ROLE_ADMIN')"))]
final class SeasonController extends AbstractController
{
    public function __construct(
        private readonly SeasonRepository $seasonRepository,
    ) {}

    #[Route('', name: 'admin_season')]
    public function index(): Response
    {
        $seasons = $this->seasonRepository->findBy([], ['position' => 'ASC']);

        return $this->render('@CpCreationVitiCore/admin/season/index.html.twig', [
            'items' => $seasons,
        ]);
    }

    #[Route('/new', name: 'admin_season_new')]
    public function new(Request $request): Response
    {
        $form = $this->createForm(SeasonType::class);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            /** @var Season $data */
            $data = $form->getData();

            $this->seasonRepository->save($data);
            $this->addFlash('alert-success', 'La saison '.$data->getTitle().' a été ajoutée avec succès');

            return $this->redirectToRoute('admin_season');
        }

        return $this->render('@CpCreationVitiCore/admin/season/new.html.twig', [
            'form' => $form->createView(),
        ]);
    }

    #[Route('/{id}/edit', name: 'admin_season_edit')]
    public function edit(Season $season, Request $request): Response
    {
        $form = $this->createForm(SeasonType::class, $season);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            /** @var Season $data */
            $data = $form->getData();

            $data->setUpdatedAt(new \DateTimeImmutable()); // Forcer Update de doctrine
            $this->seasonRepository->save($data);

            $this->addFlash('alert-success', 'La saison '.$season->getTitle().' a été modifiée avec succès');

            return $this->redirectToRoute('admin_season');
        }

        return $this->render('@CpCreationVitiCore/admin/season/edit.html.twig', [
            'item' => $season,
            'form' => $form->createView(),
        ]);
    }

    #[Route('/{id}/delete', name: 'admin_season_delete')]
    public function delete(Season $season): Response
    {
        $this->seasonRepository->delete($season);
        $this->addFlash('alert-success', 'La saison '.$season->getTitle().' a été supprimée avec succès.');

        return $this->redirectToRoute('admin_season');
    }

    #[Route('/{id}/moveUp', name: 'admin_season_move_up')]
    public function moveUp(Season $season): Response
    {
        $season->setPosition($season->getPosition() - 1);
        $this->seasonRepository->save($season);

        $this->addFlash('alert-success', 'La saison '.$season->getTitle().' a été remontée.');

        return $this->redirectToRoute('admin_season');
    }

    #[Route('/{id}/moveDown', name: 'admin_season_move_down')]
    public function moveDown(Season $season): Response
    {
        $season->setPosition($season->getPosition() + 1);
        $this->seasonRepository->save($season);

        $this->addFlash('alert-success', 'La saison '.$season->getTitle().' a été descendue.');

        return $this->redirectToRoute('admin_season');
    }
}
