<?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\DepotType;
use CpCreation\VitiCore\Depot\Model\Depot;
use CpCreation\VitiCore\Depot\Repository\DepotRepository;
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_DEPOT')"))]
#[Route('/depots')]
class DepotController extends AbstractController
{
    public function __construct(private readonly DepotRepository $depotRepository)
    {
    }

    #[Route('', name: 'admin_depot')]
    public function index(): Response
    {
        $depots = $this->depotRepository->findAllOrderByCountryOrderByCategoryOrderPositionAdmin();

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

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

        if ($form->isSubmitted() && $form->isValid()) {
            /** @var Depot $data */
            $data = $form->getData();
            $this->depotRepository->save($data);
            $this->addFlash('alert-success', 'Le dépôt '.$data->getName().' a été ajouté avec succès');

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

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

    #[Route('/{id}/edit', name: 'admin_depot_edit')]
    public function edit(Depot $depot, Request $request): Response
    {
        $form = $this->createForm(DepotType::class, $depot);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            /** @var Depot $data */
            $data = $form->getData();
            $this->depotRepository->save($data);
            $this->addFlash('alert-success', 'Le dépôt '.$depot->getName().' a été modifié avec succès');

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

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

    #[Route('/{id}/delete', name: 'admin_depot_delete')]
    public function delete(Depot $depot): RedirectResponse
    {
        $this->depotRepository->delete($depot);
        $this->addFlash('alert-success', 'Le dépôt '.$depot->getName().' a été supprimé avec succès.');

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

    #[Route('/{id}/disable', name: 'admin_depot_disable')]
    public function disable(Depot $depot): RedirectResponse
    {
        $depot->setDisabled(true);
        $this->depotRepository->save($depot);
        $this->addFlash('alert-success', 'Le dépôt '.$depot->getName().' a été dépublié.');

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

    #[Route('/{id}/enable', name: 'admin_depot_enable')]
    public function enable(Depot $depot): RedirectResponse
    {
        $depot->setDisabled(false);
        $this->depotRepository->save($depot);
        $this->addFlash('alert-success', 'Le dépôt '.$depot->getName().' a été publié.');

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

    #[Route('/{id}/moveUp', name: 'admin_depot_move_up')]
    public function moveUp(Depot $depot): RedirectResponse
    {
        $depot->setPosition($depot->getPosition() - 1);
        $this->depotRepository->save($depot);
        $this->addFlash('alert-success', 'Le dépôt '.$depot->getName().' a été remonté.');

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

    #[Route('/{id}/moveDown', name: 'admin_depot_move_down')]
    public function moveDown(Depot $depot): RedirectResponse
    {
        $depot->setPosition($depot->getPosition() + 1);
        $this->depotRepository->save($depot);
        $this->addFlash('alert-success', 'Le dépôt '.$depot->getName().' a été descendu.');

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