<?php

/*
 * This file is part of the Serenity package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

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

use CpCreation\VitiCore\Award\Model\Award;
use CpCreation\VitiCore\Award\Repository\AwardRepository;
use CpCreation\VitiCore\Bridge\Symfony\Bundle\Form\Admin\AwardType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;

#[IsGranted(new Expression("is_granted('ROLE_AWARD') or is_granted('ROLE_ADMIN')"))]
#[Route('/recompenses')]
class AwardController extends AbstractController
{
    public function __construct(private readonly AwardRepository $awardRepository)
    {
    }

    #[Route('', name: 'admin_award')]
    public function index(): Response
    {
        $awards = $this->awardRepository->findByCreatedAdminAt('DESC');

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

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

        if ($form->isSubmitted() && $form->isValid()) {
            /** @var Award $data */
            $data = $form->getData();
            $this->awardRepository->save($data);

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

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

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

    #[Route('/{id}/edit', name: 'admin_award_edit')]
    public function edit(Award $award, Request $request): Response
    {
        $form = $this->createForm(AwardType::class, $award);
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            /** @var Award $data */
            $data = $form->getData();
            $data->setUpdatedAt(new \DateTime()); // Forcer Update de doctrine
            $this->awardRepository->save($data);
            $this->addFlash('alert-success', 'La récompense '.$award->getTitle().' a été modifié avec succès');

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

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

    #[Route('/{id}/delete', name: 'admin_award_delete')]
    public function delete(Award $award): RedirectResponse
    {
        $this->awardRepository->delete($award);
        $this->addFlash('alert-success', 'La récompense '.$award->getTitle().' a été supprimé avec succès.');

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