<?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\LibraryType;
use CpCreation\VitiCore\Library\Model\Library;
use CpCreation\VitiCore\Library\Repository\LibraryRepository;
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('/library')]
final class LibraryController extends AbstractController
{
    public function __construct(
        private readonly LibraryRepository $libraryRepository,
    ) {
    }

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

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

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

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

            $this->libraryRepository->save($library);
            $this->addFlash('alert-success', \sprintf('La photo %s a été ajoutée avec succès.', $library->getTitle()));

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

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

    #[Route('/{id}/edit', name: 'admin_library_edit')]
    public function edit(Library $library, Request $request): Response
    {
        $form = $this->createForm(LibraryType::class, $library);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $this->libraryRepository->save($library);
            $this->addFlash('alert-success', \sprintf('La photo %s a été modifiée avec succès.', $library->getTitle()));

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

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

    #[Route('/{id}/delete', name: 'admin_library_delete')]
    public function delete(Library $library, Request $request): RedirectResponse
    {
        $this->libraryRepository->delete($library);
        $this->addFlash('alert-success', \sprintf('La photo %s a été supprimée avec succès.', $library->getTitle()));

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

    #[Route('/{id}/moveUp', name: 'admin_library_move_up')]
    public function moveUp(Library $library, Request $request): RedirectResponse
    {
        $library->setPosition($library->getPosition() - 1);
        $this->libraryRepository->save($library);
        $this->addFlash('alert-success', \sprintf('La photo %s a été remontée.', $library->getTitle()));

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

    #[Route('/{id}/moveDown', name: 'admin_library_move_down')]
    public function moveDown(Library $library, Request $request): RedirectResponse
    {
        $library->setPosition($library->getPosition() + 1);
        $this->libraryRepository->save($library);
        $this->addFlash('alert-success', \sprintf('La photo %s a été descendue.', $library->getTitle()));

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