<?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\Bridge\Symfony\Bundle\Form\Admin\EventType;
use CpCreation\VitiCore\Event\Model\Event;
use CpCreation\VitiCore\Event\Repository\EventRepository;
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_ADMIN') or is_granted('ROLE_EVENT')"))]
#[Route('/events')]
class EventController extends AbstractController
{
    public function __construct(private EventRepository $eventRepository)
    {
    }

    #[Route('', name: 'admin_event')]
    public function index(): Response
    {
        $events = $this->eventRepository->findBy([], ['beginAt' => 'ASC']);

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

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

        if ($form->isSubmitted() && $form->isValid()) {
            /** @var Event $data */
            $data = $form->getData();
            $this->eventRepository->save($data);
            $this->addFlash('alert-success', "L\'événement ".$data->getName().' a été ajouté avec succès');

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

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

    #[Route('/{id}/edit', name: 'admin_event_edit')]
    public function edit(Event $event, Request $request): Response
    {
        $form = $this->createForm(EventType::class, $event);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            /** @var Event $data */
            $data = $form->getData();
            $data->setUpdatedAt(new \DateTime());
            $this->eventRepository->save($data);
            $this->addFlash('alert-success', 'L\'événement '.$event->getName().' a été modifié avec succès');

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

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

    #[Route('/{id}/delete', name: 'admin_event_delete')]
    public function delete(Event $event): RedirectResponse
    {
        $this->eventRepository->delete($event);
        $this->addFlash('alert-success', 'L\'événement '.$event->getName().' a été  supprimé avec succès.');

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

    #[Route('/{id}/disable', name: 'admin_event_disable')]
    public function disable(Event $event): RedirectResponse
    {
        $event->setDisabled(1);
        $this->eventRepository->save($event);
        $this->addFlash('alert-success', 'L\'événement '.$event->getName().' a été dépublié.');

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

    #[Route('/{id}/enable', name: 'admin_event_enable')]
    public function enable(Event $event): RedirectResponse
    {
        $event->setDisabled(0);
        $this->eventRepository->save($event);
        $this->addFlash('alert-success', 'L\'événement '.$event->getName().' a été publié.');

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