<?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\HistoryType;
use CpCreation\VitiCore\History\Model\History;
use CpCreation\VitiCore\History\Model\HistoryImage;
use CpCreation\VitiCore\History\Repository\HistoryRepository;
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('/history')]
class HistoryController extends AbstractController
{
    public function __construct(private HistoryRepository $historyRepository)
    {
    }

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

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

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

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

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

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

    #[Route('/{id}/edit', name: 'admin_history_edit')]
    public function edit(History $history, Request $request): Response
    {
        $form = $this->createForm(HistoryType::class, $history);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            /** @var History $data */
            $data = $form->getData();
            $data->setUpdatedAt(new \DateTime()); // Forcer Update de doctrine
            /** @var HistoryImage $image */
            foreach ($data->getImages() as $key => $image) {
                $image->setHistory($history);
            }
            $this->historyRepository->save($data);
            $this->addFlash('alert-success', 'L\'histoire '.$history->getTitle().' a été modifié avec succès');

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

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

    #[Route('/{id}/delete', name: 'admin_history_delete')]
    public function delete(History $history): RedirectResponse
    {
        $this->historyRepository->delete($history);
        $this->addFlash('alert-success', 'L\'histoire '.$history->getTitle().' a été supprimé avec succès.');

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

    #[Route('/{id}/moveUp', name: 'admin_history_move_up')]
    public function moveUp(History $history): RedirectResponse
    {
        $history->setPosition($history->getPosition() - 1);
        $this->historyRepository->save($history);
        $this->addFlash('alert-success', 'L\'histoire '.$history->getTitle().' a été remonté.');

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

    #[Route('/{id}/moveDown', name: 'admin_history_move_down')]
    public function moveDownAction(History $history): RedirectResponse
    {
        $history->setPosition($history->getPosition() + 1);
        $this->historyRepository->save($history);
        $this->addFlash('alert-success', 'L\'histoire '.$history->getTitle().' a été descendu.');

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