<?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\GiteType;
use CpCreation\VitiCore\Gite\Model\Gite;
use CpCreation\VitiCore\Gite\Repository\GiteRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
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('ROLE_ADMIN')]
#[Route('/gite')]
class GiteController extends AbstractController
{
    public function __construct(private GiteRepository $giteRepository)
    {
    }

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

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

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

        if ($form->isSubmitted() && $form->isValid()) {
            /** @var Gite $data */
            $data = $form->getData();
            $this->giteRepository->save($data);
            $this->addFlash('alert-success', 'Le gite '.$data->getTitle().' a été ajouté avec succès');

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

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

    #[Route('/{id}/edit', name: 'admin_gite_edit')]
    public function edit(Gite $gite, Request $request): Response
    {
        $form = $this->createForm(GiteType::class, $gite);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            /** @var Gite $data */
            $data = $form->getData();
            $data->setUpdatedAt(new \DateTime()); // Forcer Update de doctrine
            $this->giteRepository->save($data);
            $this->addFlash('alert-success', 'Le gite '.$gite->getTitle().' a été modifié avec succès');

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

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

    #[Route('/{id}/moveUp', name: 'admin_gite_move_up')]
    public function moveUp(Gite $gite): RedirectResponse
    {
        $gite->setPosition($gite->getPosition() - 1);
        $this->giteRepository->save($gite);
        $this->addFlash('alert-success', 'Le gite '.$gite->getTitle().' a été remonté.');

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

    #[Route('/{id}/moveDown', name: 'admin_gite_move_down')]
    public function moveDown(Gite $gite): RedirectResponse
    {
        $gite->setPosition($gite->getPosition() + 1);
        $this->giteRepository->save($gite);
        $this->addFlash('alert-success', 'Le gite '.$gite->getTitle().' a été descendu.');

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

    #[Route('/{id}/delete', name: 'admin_gite_delete')]
    public function delete(Gite $gite): RedirectResponse
    {
        $this->giteRepository->delete($gite);
        $this->addFlash('alert-success', 'Le gite '.$gite->getTitle().' a été supprimé avec succès.');

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