<?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\DeliveryMethodType;
use CpCreation\VitiCore\Shop\Model\DeliveryMethod;
use CpCreation\VitiCore\Shop\Model\DeliveryMethodCategory;
use CpCreation\VitiCore\Shop\Repository\DeliveryMethodRepository;
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_SHIPPING_METHOD')"))]
#[Route('/delivery-method')]
class DeliveryMethodController extends AbstractController
{
    public function __construct(private DeliveryMethodRepository $deliveryMethodRepository)
    {
    }

    #[Route('', name: 'admin_delivery_method')]
    public function index(): Response
    {
        $methods = $this->deliveryMethodRepository->findByPositionOrderAdmin();

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

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

        if ($form->isSubmitted() && $form->isValid()) {
            /** @var DeliveryMethod $data */
            $data = $form->getData();
            $this->deliveryMethodRepository->save($data);
            $this->addFlash('alert-success', 'La méthode de livraison a été ajoutée avec succès');

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

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

    #[Route('/{id}/edit', name: 'admin_delivery_method_edit')]
    public function edit(DeliveryMethod $method, Request $request): Response
    {
        $form = $this->createForm(DeliveryMethodType::class, $method);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            /** @var DeliveryMethod $data */
            $data = $form->getData();
            $this->deliveryMethodRepository->save($data);
            $this->addFlash('alert-success', 'La méthode de livraison a été modifié avec succès');

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

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

    #[Route('/{id}/delete', name: 'admin_delivery_method_delete')]
    public function delete(DeliveryMethod $method): RedirectResponse
    {
        $this->deliveryMethodRepository->softDelete($method);
        $this->addFlash('alert-success', 'La méthode de livraison a été supprimé avec succès.');

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

    #[Route('/{id}/move-up', name: 'admin_delivery_method_move_up')]
    public function moveUp(DeliveryMethod $method): RedirectResponse
    {
        $method->setPosition($method->getPosition() - 1);
        $this->deliveryMethodRepository->save($method);
        $this->addFlash('alert-success', 'La méthode de livraison '.$method->getName().' a été remonté.');

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

    #[Route('/{id}/move-down', name: 'admin_delivery_method_move_down')]
    public function moveDown(DeliveryMethod $method): RedirectResponse
    {
        $method->setPosition($method->getPosition() + 1);
        $this->deliveryMethodRepository->save($method);
        $this->addFlash('alert-success', 'La méthode de livraison '.$method->getName().' a été descendu.');

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

    #[Route('/{id}/disable', name: 'admin_delivery_method_disable')]
    public function disable(DeliveryMethod $method): RedirectResponse
    {
        $method->setDisabled(true);
        $this->deliveryMethodRepository->save($method);
        $this->addFlash('alert-success', 'La méthode de livraison '.$method->getName().' a été dépublié.');

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

    #[Route('/{id}/enable', name: 'admin_delivery_method_enable')]
    public function enableAction(DeliveryMethod $method): RedirectResponse
    {
        $method->setDisabled(false);
        $this->deliveryMethodRepository->save($method);
        $this->addFlash('alert-success', 'La méthode de livraison '.$method->getName().' a été publié.');

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