<?php

namespace App\Controller\Dashboard;

use App\Entity\Product;
use App\Entity\ProductPointControll;
use App\Entity\TemplatePointControle;
use App\Form\TemplatePointControleType;
use App\Repository\TemplatePointControleRepository;
use Doctrine\ORM\EntityManagerInterface;
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;

#[Route('/crm/template-point-controle')]
#[IsGranted('ROLE_ADMIN')]
class TemplatePointControleController extends AbstractController
{
    #[Route('/', name: 'app_template_point_controle_index', methods: ['GET'])]
    public function index(TemplatePointControleRepository $templatePointControleRepository): Response
    {
        return $this->render('dashboard/template_point_controle/index.twig', [
            'templates' => $templatePointControleRepository->findAll(),
        ]);
    }

    #[Route('/new', name: 'app_template_point_controle_new', methods: ['GET', 'POST'])]
    public function new(Request $request, EntityManagerInterface $entityManager): Response
    {
        $template = new TemplatePointControle();
        
        if ($request->isMethod('POST')) {
            $name = $request->request->get('name');
            $pointsStr = $request->request->get('points');
            
            $points = array_filter(array_map('trim', explode("
", $pointsStr)));
            
            $template->setName($name);
            $template->setPoints(array_values($points));
            
            $entityManager->persist($template);
            $entityManager->flush();

            return $this->redirectToRoute('app_template_point_controle_index', [], Response::HTTP_SEE_OTHER);
        }

        return $this->render('dashboard/template_point_controle/new.twig', [
            'template' => $template,
        ]);
    }

    #[Route('/{id}/edit', name: 'app_template_point_controle_edit', methods: ['GET', 'POST'])]
    public function edit(Request $request, TemplatePointControle $template, EntityManagerInterface $entityManager): Response
    {
        if ($request->isMethod('POST')) {
            $name = $request->request->get('name');
            $pointsStr = $request->request->get('points');
            
            $points = array_filter(array_map('trim', explode("
", $pointsStr)));
            
            $template->setName($name);
            $template->setPoints(array_values($points));
            
            $entityManager->flush();

            return $this->redirectToRoute('app_template_point_controle_index', [], Response::HTTP_SEE_OTHER);
        }

        return $this->render('dashboard/template_point_controle/edit.twig', [
            'template' => $template,
        ]);
    }

    #[Route('/{id}', name: 'app_template_point_controle_delete', methods: ['POST'])]
    public function delete(Request $request, TemplatePointControle $template, EntityManagerInterface $entityManager): Response
    {
        if ($this->isCsrfTokenValid('delete'.$template->getId(), $request->request->get('_token'))) {
            $entityManager->remove($template);
            $entityManager->flush();
        }

        return $this->redirectToRoute('app_template_point_controle_index', [], Response::HTTP_SEE_OTHER);
    }

    #[Route('/apply/{id}/product/{productId}', name: 'app_template_point_controle_apply', methods: ['POST'])]
    public function apply(TemplatePointControle $template, int $productId, EntityManagerInterface $entityManager): Response
    {
        $product = $entityManager->getRepository(Product::class)->find($productId);
        
        if (!$product) {
            throw $this->createNotFoundException('Product not found');
        }

        foreach ($template->getPoints() as $pointName) {
            $point = new ProductPointControll();
            $point->setName($pointName);
            $point->setProduct($product);
            $entityManager->persist($point);
        }

        $entityManager->flush();

        $this->addFlash('success', 'Template appliqué avec succès !');

        return $this->redirectToRoute('app_crm_product_edit', ['id' => $product->getId()]);
    }
}
