<?php

namespace App\Controller;

use App\Entity\Utilisateurs;
use App\Form\UpdateUserType;
use App\Repository\UtilisateursRepository;
use App\Form\AddUserFormType;
use App\Security\LoginAuthenticator;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;

class UserController extends AbstractController
{
    public function __construct(UtilisateursRepository $utilisateursRepository)
    {
        $this->utilisateursRepository = $utilisateursRepository;
    }

    #[Route('/user/add', name: 'add_user')]
    public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, Security $security, EntityManagerInterface $entityManager): Response
    {
        $user = new Utilisateurs();
        $form = $this->createForm(AddUserFormType::class, $user);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            /** @var string $plainPassword */
            $plainPassword = $form->get('Password')->getData();

            // encode the plain password
            $user->setPassword($userPasswordHasher->hashPassword($user, $plainPassword));

            $entityManager->persist($user);
            $entityManager->flush();

            // do anything else you need here, like send an email

            return $security->login($user, LoginAuthenticator::class, 'main');
        }

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

    #[Route('/user/update/{id}', name: 'update-user')]
    public function update(Request $request, UserPasswordHasherInterface $userPasswordHasher, Security $security, EntityManagerInterface $entityManager, int $id): Response
    {
        $user = $this->utilisateursRepository->find($id);
        $form = $this->createForm(UpdateUserType::class, $user);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            /** @var string $plainPassword */
            $plainPassword = $form->get('Password')->getData();

            // encode the plain password
            $user->setPassword($userPasswordHasher->hashPassword($user, $plainPassword));

            $entityManager->persist($user);
            $entityManager->flush();

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

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


    #[Route('/user/list', name: 'list_user')]
    public function list(): Response
    {
        $utilisateur = $this->utilisateursRepository->findAll();

        return $this->render('user/list.html.twig', [
            'utilisateurs' => $utilisateur
        ]);
    }

    #[Route('/gestion-user', name: 'gestion_user', methods: ['GET'])]
    public function index(): Response
    {
        return $this->render('user/gestion.html.twig');
    }
}
