<?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\LocaleType;
use CpCreation\VitiCore\Locale\Model\Locale;
use CpCreation\VitiCore\Locale\Repository\LocaleRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
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('/langues')]
final class LocaleController extends AbstractController
{
    public function __construct(
        private readonly LocaleRepository $localeRepository,
    ) {
    }

    #[Route('', name: 'admin_locale')]
    public function index(): Response
    {
        $locales = $this->localeRepository->findBy([], ['name' => 'ASC']);

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

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

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

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

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

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

    #[Route('/{id}/edit', name: 'admin_locale_edit')]
    public function edit(Locale $locale, Request $request): Response
    {
        $form = $this->createForm(LocaleType::class, $locale);
        $form->handleRequest($request);

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

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

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

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

    #[Route('/{id}/moveUp', name: 'admin_locale_move_up')]
    public function moveUp(Locale $locale): Response
    {
        $locale->setPosition($locale->getPosition() - 1);
        $this->localeRepository->save($locale);

        $this->addFlash('alert-success', 'La locale '.$locale->getName().' a été remontée.');

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

    #[Route('/{id}/moveDown', name: 'admin_locale_move_down')]
    public function moveDown(Locale $locale): Response
    {
        $locale->setPosition($locale->getPosition() + 1);
        $this->localeRepository->save($locale);

        $this->addFlash('alert-success', 'La locale '.$locale->getName().' a été descendue.');

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

    #[Route('/{id}/delete', name: 'admin_locale_delete')]
    public function delete(Locale $locale): Response
    {
        $this->localeRepository->delete($locale);

        $this->addFlash('alert-success', 'La locale '.$locale->getName().' a été supprimée avec succès.');

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