<?php

namespace App\Controller;

use App\Entity\Buy;
use App\Entity\Participant;
use App\Entity\PhotoCource;
use App\Entity\Rating;
use App\Entity\PhotoInstitution;
use App\Form\ParticipantType;
use App\Repository\ParticipantRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @Route("/admin/participant")
 */
class ParticipantController extends AbstractController
{
    /**
     * @Route("/", name="participant_index", methods={"GET"})
     */
    public function index(ParticipantRepository $participantRepository): Response
    {
        $user = $this->getUser();
        $em = $this->getDoctrine();
        $ins= $em->getRepository(PhotoInstitution::class)->findOneBy(array("user" => $user));
        return $this->render('participant/index.html.twig', [
            'participants' => $participantRepository->findAll(),
            'institution' => $ins
        ]);
    }

    /**
     * @Route("/new", name="participant_new", methods={"GET","POST"})
     */
    public function new(Request $request): Response
    {
        $participant = new Participant();
        $form = $this->createForm(ParticipantType::class, $participant);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($participant);
            $entityManager->flush();

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

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

    /**
     * @Route("/{id}", name="participant_show", methods={"GET"})
     */
    public function show(Participant $participant): Response
    {
        return $this->render('participant/show.html.twig', [
            'participant' => $participant,
        ]);
    }

    /**
     * @Route("/{id}/edit", name="participant_edit", methods={"GET","POST"})
     */
    public function edit(Request $request, Participant $participant): Response
    {
        $form = $this->createForm(ParticipantType::class, $participant);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $this->getDoctrine()->getManager()->flush();

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

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

    /**
     * @Route("/{id}", name="participant_delete", methods={"DELETE"})
     */
    public function delete(Request $request, Participant $participant): Response
    {
        if ($this->isCsrfTokenValid('delete'.$participant->getId(), $request->request->get('_token'))) {
            $Buys= $this->getDoctrine()
                ->getRepository(Buy::class)
                ->findBy(['Participant' =>$participant]);
            foreach ($Buys as $index => $buy) {
                $entityManager = $this->getDoctrine()->getManager();
                $entityManager->remove($buy);
                $entityManager->flush();
            }
            $Ratings= $this->getDoctrine()
                ->getRepository(Rating::class)
                ->findBy(['Participant' =>$participant]);
            foreach ($Ratings as $index => $Rating) {
                $entityManager = $this->getDoctrine()->getManager();
                $entityManager->remove($Rating);
                $entityManager->flush();
            }
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->remove($participant);
            $entityManager->flush();
        }

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