<?php

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

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Form\Admin\PaymentMethodFlow;
use CpCreation\VitiCore\Cart\Model\PaymentMethod;
use CpCreation\VitiCore\Cart\Repository\PaymentMethodRepository;
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('/methode-paiement')]
#[IsGranted(new Expression("is_granted('ROLE_ADMIN')"))]
final class PaymentMethodController extends AbstractController
{
    public function __construct(
        private readonly PaymentMethodRepository $paymentMethodRepository,
        private readonly PaymentMethodFlow $paymentMethodFlow,
    ) {}

    #[Route('', name: 'admin_payment')]
    public function index(): Response
    {
        $payments = $this->paymentMethodRepository->findByPositionOrderAdmin();

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

    #[Route('/new', name: 'admin_payment_new')]
    public function new(Request $request): Response
    {
        $formData = new PaymentMethod();

        $flow = $this->paymentMethodFlow;
        $flow->bind($formData);

        $form = $flow->createForm();

        if ($flow->isValid($form)) {
            $flow->saveCurrentStepData($form);

            if ($flow->nextStep()) {
                $form = $flow->createForm();
            } else {
                /** @var PaymentMethod $data */
                $data = $form->getData();

                $this->paymentMethodRepository->save($data);
                $this->addFlash('alert-success', 'La méthode de paiement '.$data->getMethod().' a été ajoutée avec succès');
                $flow->reset();

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

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

    #[Route('/{id}/edit', name: 'admin_payment_edit')]
    public function edit(PaymentMethod $method, Request $request): Response
    {
        $flow = $this->paymentMethodFlow;
        $flow->bind($method);

        $form = $flow->createForm();

        if ($flow->isValid($form)) {
            $flow->saveCurrentStepData($form);

            if ($flow->nextStep()) {
                $form = $flow->createForm();
            } else {
                /** @var PaymentMethod $data */
                $data = $form->getData();

                $this->paymentMethodRepository->save($data);
                $this->addFlash('alert-success', 'La méthode de paiement '.$data->getMethod().' a été modifiée avec succès');
                $flow->reset();

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

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

    #[Route('/{id}/disable', name: 'admin_payment_disable')]
    public function disable(PaymentMethod $method): Response
    {
        $method->setDisabled(true);
        $this->paymentMethodRepository->save($method);

        $this->addFlash('alert-success', 'Le mode de paiement a été dépublié.');

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

    #[Route('/{id}/enable', name: 'admin_payment_enable')]
    public function enable(PaymentMethod $method): Response
    {
        $method->setDisabled(false);
        $this->paymentMethodRepository->save($method);

        $this->addFlash('alert-success', 'Le mode de paiement a été publié.');

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

    #[Route('/{id}/moveUp', name: 'admin_payment_move_up')]
    public function moveUp(PaymentMethod $method): Response
    {
        $method->setPosition($method->getPosition() - 1);
        $this->paymentMethodRepository->save($method);

        $this->addFlash('alert-success', 'Le mode de paiement '.$method->getMethod().' a été remonté.');

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

    #[Route('/{id}/moveDown', name: 'admin_payment_move_down')]
    public function moveDown(PaymentMethod $method): Response
    {
        $method->setPosition($method->getPosition() + 1);
        $this->paymentMethodRepository->save($method);

        $this->addFlash('alert-success', 'Le mode de paiement '.$method->getMethod().' a été descendu.');

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

    #[Route('/{id}/delete', name: 'admin_payment_delete')]
    public function delete(PaymentMethod $method): Response
    {
        $this->paymentMethodRepository->softDelete($method);

        $this->addFlash('alert-success', 'Le mode de paiement '.$method->getMethod().' a été supprimé avec succès.');

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