<?php

/*
 * This file is part of the Serenity package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

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

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Form\Admin\LabelType;
use CpCreation\VitiCore\Contact\Model\Label;
use CpCreation\VitiCore\Contact\Repository\LabelRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;

#[IsGranted('ROLE_ADMIN')]
#[Route('/label')]
class LabelController extends AbstractController
{
    public function __construct(private LabelRepository $labelRepository)
    {
    }

    #[Route('', name: 'admin_label')]
    public function index(): Response
    {
        $labels = $this->labelRepository->findBy([], ['position' => 'ASC']);

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

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

        if ($form->isSubmitted() && $form->isValid()) {
            /** @var Label $data */
            $data = $form->getData();
            $this->labelRepository->save($data);
            $this->addFlash('alert-success', 'Le label a été ajouté avec succès');

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

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

    #[Route('/{id}/edit', name: 'admin_label_edit')]
    public function edit(Label $label, Request $request): Response
    {
        $form = $this->createForm(LabelType::class, $label);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            /** @var Label $data */
            $data = $form->getData();
            $this->labelRepository->save($data);
            $this->addFlash('alert-success', 'Le label a été modifié avec succès');

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

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

    #[Route('/{id}/delete', name: 'admin_label_delete')]
    public function delete(Label $label): RedirectResponse
    {
        $this->labelRepository->delete($label);
        $this->addFlash('alert-success', 'Le label a été supprimé avec succès.');

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

    #[Route('/{id}/moveUp', name: 'admin_label_move_up')]
    public function moveUp(Label $label): RedirectResponse
    {
        $label->setPosition($label->getPosition() - 1);
        $this->labelRepository->save($label);
        $this->addFlash('alert-success', 'Le label a été remonté.');

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

    #[Route('/{id}/moveDown', name: 'admin_label_move_down')]
    public function moveDown(Label $label): RedirectResponse
    {
        $label->setPosition($label->getPosition() + 1);
        $this->labelRepository->save($label);
        $this->addFlash('alert-success', 'Le label a été descendu.');

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