<?php

/*
 * This file is part of the VITICORE2 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\ProductCollectionType;
use CpCreation\VitiCore\Product\Model\ProductCollection;
use CpCreation\VitiCore\Product\Repository\ProductCollectionRepository;
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('/product-collection')]
#[IsGranted(new Expression("is_granted('ROLE_ADMIN')"))]
final class ProductCollectionController extends AbstractController
{
    public function __construct(
        private readonly ProductCollectionRepository $collectionRepository,
    ) {
    }

    #[Route('', name: 'admin_product_collection')]
    public function index(): Response
    {
        return $this->render('@CpCreationVitiCore/admin/product/collection/index.html.twig', [
            'items' => $this->collectionRepository->findByCreatedAdminAt('ASC'),
        ]);
    }

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

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

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

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

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

    #[Route('/{id}/edit', name: 'admin_product_collection_edit')]
    public function edit(ProductCollection $collection, Request $request): Response
    {
        $form = $this->createForm(ProductCollectionType::class, $collection);
        $form->handleRequest($request);

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

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

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

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

    #[Route('/{id}/delete', name: 'admin_product_collection_delete')]
    public function delete(ProductCollection $collection): Response
    {
        $this->collectionRepository->delete($collection);
        $this->addFlash('alert-success', 'La collection '.$collection->getName().' a été supprimée avec succès.');

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