<?php

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

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Form\Admin\ShippingCountryType;
use CpCreation\VitiCore\Shop\Model\ShippingCountry;
use CpCreation\VitiCore\Shop\Repository\ShippingCountryRepository;
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-countries')]
#[IsGranted(new Expression("is_granted('ROLE_ADMIN')"))]
final class ShippingCountryController extends AbstractController
{
    public function __construct(
        private readonly ShippingCountryRepository $shippingCountryRepository,
    ) {}

    #[Route('', name: 'admin_shipping_country')]
    public function index(): Response
    {
        $countries = $this->shippingCountryRepository->findCountriesOrder();

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

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

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

            $this->shippingCountryRepository->save($data);
            $this->addFlash('alert-success', 'Le pays '.$data->getName().' a été ajouté avec succès');

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

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

    #[Route('/{id}/edit', name: 'admin_shipping_country_edit')]
    public function edit(ShippingCountry $country, Request $request): Response
    {
        $form = $this->createForm(ShippingCountryType::class, $country);
        $form->handleRequest($request);

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

            $this->shippingCountryRepository->save($data);
            $this->addFlash('alert-success', 'Le pays '.$country->getName().' a été modifié avec succès');

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

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

    #[Route('/{id}/delete', name: 'admin_shipping_country_delete')]
    public function delete(ShippingCountry $country): Response
    {
        $this->shippingCountryRepository->delete($country);
        $this->addFlash('alert-success', 'Le pays '.$country->getName().' a été supprimé avec succès.');

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

    #[Route('/{id}/disable', name: 'admin_shipping_country_disable')]
    public function disable(ShippingCountry $country): Response
    {
        $country->setDisabled(true);
        $this->shippingCountryRepository->save($country);

        $this->addFlash('alert-success', 'Le pays '.$country->getName().' a été dépublié.');

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

    #[Route('/{id}/enable', name: 'admin_shipping_country_enable')]
    public function enable(ShippingCountry $country): Response
    {
        $country->setDisabled(false);
        $this->shippingCountryRepository->save($country);

        $this->addFlash('alert-success', 'Le pays '.$country->getName().' a été publié.');

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