<?php

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

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Form\Admin\SocialNetworkFlow;
use CpCreation\VitiCore\SocialNetwork\Model\SocialNetwork;
use CpCreation\VitiCore\SocialNetwork\Repository\SocialNetworkRepository;
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('/social-network')]
#[IsGranted(new Expression("is_granted('ROLE_ADMIN') or is_granted('ROLE_SOCIAL')"))]
final class SocialNetworkController extends AbstractController
{
    public function __construct(
        private readonly SocialNetworkRepository $socialNetworkRepository,
        private readonly SocialNetworkFlow $socialNetworkFlow,
    ) {}

    #[Route('', name: 'admin_social')]
    public function index(): Response
    {
        $socials = $this->socialNetworkRepository->findAll();

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

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

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

        $form = $flow->createForm();

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

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

                $this->socialNetworkRepository->save($data);
                $this->addFlash('alert-success', 'Le réseau social '.$data->getNetwork().' a été ajouté avec succès');
                $flow->reset();

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

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

    #[Route('/{id}/edit', name: 'admin_social_edit')]
    public function edit(SocialNetwork $social, Request $request): Response
    {
        $flow = $this->socialNetworkFlow;
        $flow->bind($social);

        $form = $flow->createForm();

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

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

                $this->socialNetworkRepository->save($data);
                $this->addFlash('alert-success', 'Le réseau social '.$data->getNetwork().' a été modifié avec succès');
                $flow->reset();

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

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

    #[Route('/{id}/delete', name: 'admin_social_delete')]
    public function delete(SocialNetwork $social): Response
    {
        $this->socialNetworkRepository->delete($social);
        $this->addFlash('alert-success', 'Le réseau social '.$social->getNetwork().' a été supprimé avec succès.');

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