<?php

declare(strict_types=1);

namespace App\Controller\Admin\Properties;

use App\Managers\ManagersInterface;
use App\Entity\Properties\Properties;
use App\Form\Properties\PropertiesType;
use App\Handlers\Forms\PropertiesHandle;
use App\Interfaces\NotificationsInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Repository\Properties\DemarcheursRepository;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security as Security;

#[Route('/admin/demarcheurs')]
#[Security("is_granted('ROLE_SUPER_ADMIN') or is_granted('ROLE_ADMIN')")]
class AdminDemarcheursController extends AbstractController
{
    public function __construct(
        private NotificationsInterface $notifications,
        private ManagersInterface $managers,
        private PropertiesHandle $handle,
        private DemarcheursRepository $demarcheursRepository
    ) {}

    #[Route('/', name: "admin.demarcheurs.index", methods: ['GET'])]
    /**
     * @param DemarcheursRepository $demarcheursRepository
     * @return Response
     */
    public function index(DemarcheursRepository $demarcheursRepository): Response
    {
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');

        $user = $this->getUser();

        // if ($this->isGranted('ROLE_SUPER_ADMIN')) {
        //     $properties = $propertiesRepository->getAll();
        // } else {
        //     $properties = $propertiesRepository->findBy([
        //         'companyId' => $user->getCompanyId()
        //     ]);
        // }

        return $this->render('admin/properties/demarcheurs/index.html.twig', [
            'demarcheurs' => $demarcheursRepository->findAllOrderByCreationDesc(),
            'title' => 'Liste des démarcheurs',
            'libelle_ajouter' => 'Nouveau démarcheur',
            'current_page' => 'demarcheurs',
            'current_global' => 'demarcheurs'
        ]);
    }

    #[Route('/new', name: "admin.demarcheurs.new", methods: ["GET", "POST"])]
    /**
     * @param Request $request
     * @return Response
     */
    public function new(Request $request): Response
    {
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
        $demarcheur = $this->repository->new();
        $form = $this->createForm(PropertiesType::class, $demarcheur, ['demarcheur' => $demarcheur]);
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            //$demarcheur->setCompanyId($this->getUser()->getCompanyId());
            $this->handle->handlePropertieForm($form);
            $this->addFlash("success", 'Démarcheur ajoutée avec succès.');
            return $this->redirectToRoute('admin.demarcheurs.index');
        }

        return $this->render('admin/properties/demarcheurs/new.html.twig', [
            'demarcheur' => $demarcheur,
            'form' => $form->createView(),
            'title' => 'Nouveau démarcheur',
            'libelle_liste' => 'Liste des démarcheurs',
            'current_page' => 'demarcheur_new',
            'current_global' => 'demarcheurs'
        ]);
    }


    #[Route('/{id<\d+>}/edit', name: "admin.properties.edit", methods: ["GET", "POST"])]
    /**
     * @param Request $request
     * @return Response
     */

    public function edit(Request $request, Properties $propertie): Response
    {
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
    }

    #[Route('/{id<\d+>}', name: "admin.properties.delete", methods: ['DELETE'])]
    /**
     * @param Request $request
     * @param Properties $propertie
     * @return Response
     */
    public function delete(Request $request, Properties $propertie): Response
    {
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');

        if ($this->isCsrfTokenValid('delete' . $propertie->getId(), $request->request->get('_token'))) {
            $this->notifications->save(
                "Suppression de la propriété #{$propertie->getName()}",
                "Suppression",
                $propertie,
                "trash",
                "red",
                $propertie->getCompanyId()
            );
            $this->managers->setEntity($propertie, 'delete');
            $this->addFlash("success", 'Appartement supprimée avec succès.');
        }

        $referer = $request->headers->get('referer');
        return new RedirectResponse($referer ?? $this->generateUrl('admin.properties.index'));
    }
}
