<?php

namespace App\Controller\BackendAdmin;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

use Doctrine\ORM\EntityManagerInterface;

use App\Entity\Partenaire;

use App\Repository\PartenaireRepository;

use App\Form\BackendAdmin\PartenaireType;

#[Route('/backend/admin/partenaire')]
class PartenaireController extends AbstractController
{

    /**
     * @var EntityManagerInterface $em
     */
    private $em;

    /**
     * @var PartenaireRepository $partenaireRepository
     */
    private $partenaireRepository;

    
    /**
     * @param EntityManagerInterface $em
     * @param PartenaireRepository $partenaireRepository
     * @return $this
     */
    public function __construct(
        EntityManagerInterface $em,
        partenaireRepository $partenaireRepository
    ){
        $this->em = $em;
        $this->partenaireRepository = $partenaireRepository;
    }

    #[Route('/index', name: 'backend_admin_partenaire_index')]
    public function index(): Response
    {
        $partenaires = $this->partenaireRepository->findAll();

        return $this->render('backend_admin/partenaire/index.html.twig', ['partenaires'=>$partenaires]);
    }
    

    #[Route('/add', name: 'backend_admin_partenaire_add')]
    public function add(Request $request): Response
    {
        $partenaire = new Partenaire();
		
		$form = $this->createForm(PartenaireType::class, $partenaire);

        $form->handleRequest($request);
		if ($form->isSubmitted() && $form->isValid()) {

			$partenaire = $form->getData();

            $partenaires = $partenaire->getPartenaire()->getPartenaires();
            foreach($partenaires as $partner){
                if($partenaire->getSociete()->getId() == $partner->getSociete()->getId()){
                    $this->addFlash('warning', ['title'=>'Attention', 'message'=>"<b>cette société existe déja pour ce utilisateur</b>"]);
                    return $this->render('backend_admin/partenaire/add.html.twig', ['form'=>$form]);
                }
            }
           
            $partenaire->setAdmin($this->getUser());
            $partenaire->setActive(true);
            $partenaire->setNotifier(true);      

            if(count($partenaires) == 0){
                $partenaire->setSelected(true); 
            }else{
                $partenaire->setSelected(false); 
            }

            // update role user
            $partner = $partenaire->getPartenaire();
            $partner->addRole("ROLE_PARTNER_ADVANCED");

            $this->em->persist($partner);
			$this->em->persist($partenaire);

			$this->em->flush();

            $this->addFlash('success', ['title'=>'Success', 'message'=>"L'ajout a été effectuée avec succès"]);
            return $this->redirectToRoute('backend_admin_partenaire_index');
		}

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


    #[Route('/activated/{id}/{active}', name: 'backend_admin_partenaire_activated')]
    public function activated(partenaire $partenaire, $active): Response
    {
        $message = "";
        $partenaire->setActive($active);
        $partenaire->setAdmin($this->getUser());
        $partenaire->setNotifier(true);

        $user = $partenaire->getPartenaire();
        $roles = $user->getRoles();

        if($active){
            $roles[] = "ROLE_PARTNER_ADVANCED";
            $message = "Le partenaire a été bien activer.";
            $societe = $partenaire->getSociete();
            $societe->setActive(true);
            $this->em->persist($societe);
            $this->em->flush();
        }else{
            $roles = [];
            $message = "Le partenaire a été bien déactiver.";
        }
       
        $user->setRoles($roles);
        $this->em->persist($user);
        $this->em->flush();

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

        $this->addFlash('success', ['title'=>'Success', 'message'=>$message]);

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

    
}