<?php

namespace CpCreation\VitiCore\Bridge\Symfony\Bundle\Controller\Admin;

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Form\Admin\ShippingAdditionalManualType;
use CpCreation\VitiCore\Shop\Model\ShippingAdditionalManual;
use CpCreation\VitiCore\Shop\Repository\ShippingAdditionalManualRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;

#[Route('/shipping-additionnal-manual')]
#[IsGranted(new Expression("is_granted('ROLE_ADMIN') or is_granted('ROLE_SHIPPING')"))]
final class ShippingAdditionalManualController extends AbstractController
{
    public function __construct(
        private readonly ShippingAdditionalManualRepository $shippingAdditionalManualRepository,
    ) {}

    #[Route('', name: 'admin_shipping_additional_manual')]
    public function index(): Response
    {
        $additionals = $this->shippingAdditionalManualRepository->findBy([], ['qtyAt' => 'ASC']);

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

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

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

            $this->shippingAdditionalManualRepository->save($data);
            $this->addFlash('alert-success', 'Le supplément a été ajouté avec succès');

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

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

    #[Route('/{id}/edit', name: 'admin_shipping_additional_manual_edit')]
    public function edit(ShippingAdditionalManual $price, Request $request): Response
    {
        $form = $this->createForm(ShippingAdditionalManualType::class, $price);
        $form->handleRequest($request);

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

            $this->shippingAdditionalManualRepository->save($data);
            $this->addFlash('alert-success', 'Le supplément a été modifié avec succès');

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

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

    #[Route('/{id}/delete', name: 'admin_shipping_additional_manual_delete')]
    public function delete(ShippingAdditionalManual $price): Response
    {
        $this->shippingAdditionalManualRepository->delete($price);
        $this->addFlash('alert-success', 'Le supplément a été supprimé avec succès.');

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