<?php

namespace App\Controller\Frontend;

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

use App\Form\BackendAdmin\SocieteType;

use Doctrine\ORM\EntityManagerInterface;

use App\Repository\SocieteRepository;

use App\Service\UploaderService;

use App\Entity\Societe;
use App\Entity\Partenaire;

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

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

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

    #[Route('/societe/add', name: 'frontend_partenaire_societe_add')]
    public function add(Request $request, UploaderService $uploaderService): Response
    {
        $societe = new Societe();
        
        $form = $this->createForm(SocieteType::class, $societe);
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            $photo = $form->get('photo')->getData();
            if ($photo) {
                $directory = $this->getParameter('societes_directory');
                $societe->setLogo($uploaderService->uploadFile($photo, $directory));
            }
            $this->em->persist($societe);
            $this->em->flush();

            $partenaire = new Partenaire();
            $partenaire->setPartenaire($this->getUser());
            $partenaire->setSociete($societe);
            $partenaire->setSelected(true);
            $partenaire->setRole(1);
            $this->em->persist($partenaire);
            $this->em->flush();

            $this->addFlash('success', ['title'=>'Success', 
                                        'message'=>"Votre demande (Devenir partenaire ) à été enregistrer et envoyer avec succées aux administateurs. 
                                                    Votre demande sera traitée au maxum 24 heur"]);
        }

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




    #[Route('/index', name: 'frontend_partenaire_index')]
    public function test(Request $request): Response
    {
        return $this->render('frontend/partenaire/index.html.twig', [
            'controller_name' => 'HomeController',
        ]);
    }
}
