<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

use Symfony\Component\Routing\Attribute\Route;

use Symfony\Contracts\Translation\TranslatorInterface;

use Doctrine\ORM\EntityManagerInterface;

use App\Entity\State;
use App\Entity\Investigation;

use App\Repository\StateRepository;
use App\Repository\InvestigationRepository;

use App\Form\InvestigationType;

#[Route('/investigation')]
final class InvestigationController extends AbstractController
{
    /**
     * @param EntityManagerInterface $em
     * @param TranslatorInterface $translator
     * @param StateRepository $stateRepository
     * @param InvestigationRepository $investigationRepository
     * @return $this
     */
    public function __construct(
        private EntityManagerInterface $em,
        private TranslatorInterface $translator,
        private StateRepository $stateRepository,
        private InvestigationRepository $investigationRepository,
    ) {
        $this->em = $em;
        $this->translator = $translator;
        $this->stateRepository = $stateRepository;
        $this->investigationRepository = $investigationRepository;
    }

    #[Route('/index', name: 'app_investigation_index')]
    public function index(): Response
    {
        $investigations = $this->investigationRepository->findBy(['create_by' => $this->getUser(), 'last_state' => [10, 11, 12, 20, 21, 22]], ["id" => 'DESC']);

        $invesAdmins = $this->investigationRepository->findBy(['create_by' => $this->getUser(), 'last_state' => 21], ["id" => 'DESC']);

        return $this->render('investigation/index.html.twig', [
            'investigations' => $investigations,
            'invesAdmins' => $invesAdmins,
        ]);
    }

    #[Route('/add', name: 'app_investigation_add')]
    public function add(Request $request): Response
    {
        $investigation = new Investigation();

        $form = $this->createForm(InvestigationType::class, $investigation);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $investigation = $form->getData();
            $investigation->setCreateBy($this->getUser());

            $this->em->persist($investigation);
            $this->em->flush();

            $this->addFlash('success', $this->translator->trans('alert_sucess_add', [], 'investigation'));
            return $this->redirectToRoute('app_investigation_index');
        }

        return $this->render('investigation/add-index.html.twig', [
            'form' => $form->createView()
        ]);
    }

    #[Route('/show/{id}', name: 'app_investigation_show')]
    public function show(Investigation $investigation): Response
    {
        return $this->render('investigation/show.html.twig', [
            'investigation' => $investigation
        ]);
    }

    #[Route('/edit/{id}', name: 'app_investigation_edit')]
    public function edit(Request $request, Investigation $investigation): Response
    {
        $form = $this->createForm(InvestigationType::class, $investigation);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) 
        {
            $investigation = $form->getData();
            $this->em->persist($investigation);
            $this->em->flush();

            $this->addFlash('success', $this->translator->trans('alert_sucess_add', [], 'investigation'));
        }

        return $this->render('investigation/add-index.html.twig', [
            'form' => $form->createView(),
            'investigation' => $investigation,
        ]);
    }

    #[Route('/duplicate/{id}', name: 'app_investigation_duplicate')]
    public function duplicate(Investigation $investigation): Response
    {
        $cloneInvestigation = clone $investigation;

        foreach ($investigation->duplicateQuestions() as $question) {
            $cloneInvestigation->addQuestion($question);
        }

        $this->em->persist($cloneInvestigation);
        $this->em->flush();

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

    #[Route('/delete/{id}', name: 'app_investigation_delete')]
    public function delete(Investigation $investigation): Response
    {
        $this->em->remove($investigation);
        $this->em->flush();

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

    #[Route('/send/validation/{id}', name: 'app_investigation_send_validation')]
    public function send_validation(Investigation $investigation): Response
    {
        $investigation->setLastState(21);
        $this->em->persist($investigation);
        $this->em->flush();

        $this->stateRepository->addState($investigation, 2, 1);

        $this->addFlash('success', $this->translator->trans('alert_sucess_send_validation', [], 'investigation'));

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

    #[Route('/ready/{id}', name: 'app_investigation_ready')]
    public function ready(Investigation $investigation): Response
    {
        $investigation->setLastState(21);
        $this->em->persist($investigation);
        $this->em->flush();

        $this->stateRepository->addState($investigation, 2, 1);

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

    #[Route('/active', name: 'app_investigation_admin_active')]
    public function active(Request $request): Response
    {
        $investigations = $this->investigationRepository->findBy(['last_state' => 21], ["id" => 'DESC']);

        $this->addFlash('success', $this->translator->trans('alert_sucess', array(), 'active'));

        $id = $request->query->get('id');
        if ($id) {
            
            $investigation = $this->investigationRepository->find($id);

            $investigation->setLastState(20);
            $this->em->persist($investigation);
            $this->em->flush();

            $this->stateRepository->addState($investigation, 2, 0);

        }

        return $this->render('investigation/admin-active.html.twig', [
            'investigations' => $investigations
        ]);
    }
}
