<?php

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

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Form\Admin\ShippingZoneType;
use CpCreation\VitiCore\Shop\Model\ShippingZone;
use CpCreation\VitiCore\Shop\Repository\ShippingZoneRepository;
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-zone')]
#[IsGranted(new Expression("is_granted('ROLE_ADMIN')"))]
final class ShippingZoneController extends AbstractController
{
    public function __construct(
        private readonly ShippingZoneRepository $shippingZoneRepository,
    ) {}

    #[Route('', name: 'admin_shipping_zone')]
    public function index(): Response
    {
        $zones = $this->shippingZoneRepository->findBy([], ['name' => 'ASC']);

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

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

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

            $this->shippingZoneRepository->save($data);
            $this->addFlash('alert-success', 'La zone '.$data->getName().' a été ajoutée avec succès');

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

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

    #[Route('/{id}/edit', name: 'admin_shipping_zone_edit')]
    public function edit(ShippingZone $zone, Request $request): Response
    {
        $form = $this->createForm(ShippingZoneType::class, $zone);
        $form->handleRequest($request);

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

            $this->shippingZoneRepository->save($data);
            $this->addFlash('alert-success', 'La zone '.$zone->getName().' a été modifiée avec succès');

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

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

    #[Route('/{id}/delete', name: 'admin_shipping_zone_delete')]
    public function delete(ShippingZone $zone): Response
    {
        $this->shippingZoneRepository->delete($zone);
        $this->addFlash('alert-success', 'La zone '.$zone->getName().' a été supprimée avec succès.');

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