<?php

namespace App\Controller\BackendAdmin;

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

use Doctrine\ORM\EntityManagerInterface;

use App\Repository\UserRepository;

use App\Entity\User;

#[Route('/backend/admin/user')]
class UserController extends AbstractController
{

    /**
     * @var UserRepository $userRepository
     */
    private $userRepository;

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

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


    #[Route('/index', name: 'backend_admin_user_index')]
    public function index(): Response
    {
        $users = $this->userRepository->findBy(['isDeleted'=> false]);
        return $this->render('backend_admin/user/index.html.twig',['users'=>$users]);
    }


    #[Route('/deleted', name: 'backend_admin_user_deleted')]
    public function deleted(): Response
    {
        $users = $this->userRepository->findBy(['isDeleted'=> true]);
        return $this->render('backend_admin/user/deleted.html.twig',['users'=>$users]);
    }

    
    #[Route('/update_delete/{id}', name: 'backend_admin_user_update_delete')]
    public function update_delete(User $user, Request $request)
    {
        
        $delete = (boolean)$request->get('delete');
        
        $user->setIsDeleted($delete);
        $this->em->persist($user);
        $this->em->flush();

        

        if($delete  == true ){
            $this->addFlash('success', ['title'=>'Success', 'message'=>'Le compte du utilisateur'.$user->getFirstname().' '.$user->getLastname().' a été bien déactiver']);

        }else{
            $this->addFlash('success', ['title'=>'Success', 'message'=>'Le compte du utilisateur'.$user->getFirstname().' '.$user->getLastname().' a été bien activer']);

        }

        $referer = $request->headers->get('referer');
        return $this->redirect($referer);
    }

}
