<?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\DepotCategoryType;
use CpCreation\VitiCore\Depot\Model\Category;
use CpCreation\VitiCore\Depot\Repository\CategoryRepository;
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('/categories_depots')]
class DepotCategoryController extends AbstractController
{
    public function __construct(private CategoryRepository $categoryRepository)
    {
    }

    #[Route('', name: 'admin_depot_category')]
    public function index(): Response
    {
        $categories = $this->categoryRepository->findByPositionOrderAdmin();

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

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

        if ($form->isSubmitted() && $form->isValid()) {
            /** @var Category $data */
            $data = $form->getData();
            $this->categoryRepository->save($data);
            $this->addFlash('alert-success', 'La catégorie '.$data->getName().' a été ajouté avec succès');

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

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

    #[Route('/{id}/edit', name: 'admin_depot_category_edit')]
    public function edit(Category $category, Request $request): Response
    {
        $form = $this->createForm(DepotCategoryType::class, $category);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            /** @var Category $data */
            $data = $form->getData();
            $this->categoryRepository->save($data);
            $this->addFlash('alert-success', 'La categorie '.$category->getName().' a été modifié avec succès');

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

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

    #[Route('/{id}/delete', name: 'admin_depot_category_delete')]
    public function delete(Category $category): RedirectResponse
    {
        $this->categoryRepository->delete($category);
        $this->addFlash('alert-success', 'La catégorie '.$category->getName().' a été supprimé avec succès.');

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

    #[Route('/{id}/moveUp', name: 'admin_depot_category_move_up')]
    public function moveUp(Category $category): RedirectResponse
    {
        $category->setPosition($category->getPosition() - 1);
        $this->categoryRepository->save($category);
        $this->addFlash('alert-success', 'La categorie '.$category->getName().' a été remonté.');

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

    #[Route('/{id}/moveDown', name: 'admin_depot_category_move_down')]
    public function moveDown(Category $category): RedirectResponse
    {
        $category->setPosition($category->getPosition() + 1);
        $this->categoryRepository->save($category);
        $this->addFlash('alert-success', 'La categorie '.$category->getName().' a été descendu.');

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