<?php

namespace App\Controller;

use App\Entity\Buy;
use App\Entity\Cource;
use App\Entity\Institution;
use App\Entity\Panier;
use App\Entity\PhotoCource;
use App\Entity\PhotoInstitution;
use App\Entity\Rating;
use App\Entity\User;
use App\Entity\Withdraw;
use App\Form\Institution1Type;
use App\Repository\InstitutionRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

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

    /**
     * @Route("/new", name="institution_new", methods={"GET","POST"})
     */
    public function new(Request $request): Response
    {
        $institution = new Institution();
        $form = $this->createForm(Institution1Type::class, $institution);
        $form->handleRequest($request);

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

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

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

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

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

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

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

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

    /**
     * @Route("/{id}", name="institution_delete", methods={"DELETE"})
     */
    public function delete(Request $request, Institution $institution): Response
    {
        if ($this->isCsrfTokenValid('delete'.$institution->getId(), $request->request->get('_token'))) {

            $Ratings= $this->getDoctrine()
                ->getRepository(Rating::class)
                ->findBy(['Institution' =>$institution]);
            foreach ($Ratings as $index => $Rating) {
                $entityManager = $this->getDoctrine()->getManager();
                $entityManager->remove($Rating);
                $entityManager->flush();
            }
            $withwards= $this->getDoctrine()
                ->getRepository(Withdraw::class)
                ->findBy(['institution' =>$institution]);
            foreach ($withwards as $index => $withward) {
                $entityManager = $this->getDoctrine()->getManager();
                $entityManager->remove($withward);
                $entityManager->flush();
            }

            $courses= $this->getDoctrine()
                ->getRepository(Cource::class)
                ->findBy(['Institution' =>$institution]);
            foreach ($courses as $index => $course) {
                $photos = $this->getDoctrine()
                    ->getRepository(PhotoCource::class)
                    ->findBy(['Cource' => $course]);
                foreach ($photos as $index => $photo) {
                    $entityManager = $this->getDoctrine()->getManager();
                    $entityManager->remove($photo);
                    $entityManager->flush();
                }
                $paniers = $this->getDoctrine()
                    ->getRepository(Panier::class)
                    ->findBy(['Cource' => $course]);
                foreach ($paniers as $index => $panier) {
                    $entityManager = $this->getDoctrine()->getManager();
                    $entityManager->remove($panier);
                    $entityManager->flush();
                }


                $entityManager = $this->getDoctrine()->getManager();
                $entityManager->remove($course);
                $entityManager->flush();
            }
            $photoinstitus= $this->getDoctrine()
                ->getRepository(PhotoInstitution::class)
                ->findBy(['institution' =>$institution]);
            foreach ($photoinstitus as $index => $photoinstitu) {
                $entityManager = $this->getDoctrine()->getManager();
                $entityManager->remove($photoinstitu);
                $entityManager->flush();
            }
            $userId=$institution->getUser();
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->remove($institution);
            $entityManager->flush();

            $user= $this->getDoctrine()
                ->getRepository(User::class)
                ->find($userId);

            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->remove($user);
            $entityManager->flush();
        }

        return $this->redirectToRoute('institution_index');
    }
    /**
     * @Route("/activeInstitution/{id}/{action}", name="activeInstitution")
     */
    public function activeInstitution($id,$action)
    {
        $User= $this->getDoctrine()
            ->getRepository(User::class)
            ->find($id);
        $entityManager = $this->getDoctrine()->getManager();
        $institution= $entityManager->getRepository(Institution::class)->findOneBy(array("user" => $User));
        $User->setIsactive($action);
        $institution->setIslogin(1);        
        $entityManager->persist($User);
        $entityManager->persist($institution);
        $entityManager->flush();

        return $this->redirectToRoute('institution_index');

    }
}
