<?php

namespace App\Controller\Dashboard;

use App\Entity\Category;
use App\Entity\Customer;
use App\Entity\CustomerAddress;
use App\Form\CategoryType;
use App\Form\CustomerAddAddressType;
use App\Form\CustomerAddType;
use App\Form\CustomerType;
use App\Logger\AppLogger;
use App\Repository\CategoryRepository;
use App\Repository\ContratsRepository;
use App\Repository\CustomerAddressRepository;
use App\Repository\CustomerRepository;
use App\Repository\DevisRepository;
use App\Service\Stripe\Client as StripeClient;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

#[Route('/crm/category')]
class CategoryController extends AbstractController
{
    public function __construct(
        private readonly EntityManagerInterface $em,
        private readonly AppLogger $appLogger,
        private readonly EventDispatcherInterface $eventDispatcher
    ) {
    }
    #[Route('', name: 'app_crm_category', options: ['sitemap' => false], methods: ['GET'])]
    public function index(
        CategoryRepository $categoryRepository,
        PaginatorInterface $paginator,
        Request $request
    ): Response {
        $this->appLogger->record('VIEW', 'Consultation de la page de  catégorie');

        foreach (['2-7 Ans','3-15 Ans','3-99 Ans','Barnums','Alimentaire','Options'] as $catName) {
            $cat = $categoryRepository->findOneBy(['name'=>$catName]);
            if(!$cat instanceof Category) {
                $cat = new Category();
                $cat->setName($catName);
                $this->em->persist($cat);
            }
            $this->em->flush();
        }

        return $this->render('dashboard/category/list.twig', [
            'categorys' => $categoryRepository->findAll(),
        ]);
    }

    #[Route('/add', name: 'app_crm_category_add', options: ['sitemap' => false], methods: ['GET', 'POST'])]
    public function add(Request $request, CategoryRepository $categoryRepository): Response
    {
        $this->appLogger->record('VIEW', 'Consultation de la page de création catégorie');

        $category = new Category();
        $form = $this->createForm(CategoryType::class, $category);
        $form->handleRequest($request);
        if($form->isSubmitted() && $form->isValid()) {
            $this->appLogger->record('VIEW', 'Création d\'une catégorie');
            $this->em->persist($category);
            $this->em->flush();
            return $this->redirectToRoute('app_crm_category');
        }
        return $this->render('dashboard/category/add.twig', [
            'form' => $form->createView(),
        ]);
    }

    #[Route('/delete/{id}', name: 'app_crm_category_delete', options: ['sitemap' => false], methods: ['GET'])]
    public function delete(
        ?Category $category,
        PaginatorInterface $paginator,
        Request $request
    ): Response {
        $this->appLogger->record('VIEW', 'Suppression de la catégorie '.$category->getName());

        $this->em->remove($category);
        $this->em->flush();
        return $this->redirectToRoute('app_crm_category');
    }
}
