<?php

declare(strict_types=1);

namespace App\Controller\Admin\Properties;

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

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

    #[Route('/', name: "admin.properties.index", methods: ['GET'])]
    /**
     * @param PropertiesRepository $propertiesRepository
     * @return Response
     */
    public function index(PropertiesRepository $propertiesRepository): Response
    {
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
        return $this->render('admin/properties/properties/index.html.twig', [
            'properties' => $propertiesRepository->getAll(),
            'title' => 'Liste des appartements',
            'libelle_ajouter' => 'Nouvel appartement',
            'current_page' => 'properties',
            'current_global' => 'properties'
        ]);
    }

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

        return $this->render('admin/properties/properties/new.html.twig', [
            'propertie' => $propertie,
            'form' => $form->createView(),
            'title' => 'Nouvel appartement',
            'libelle_liste' => 'Liste des appartements',
            'current_page' => 'properties_new',
            'current_global' => 'properties'
        ]);
    }

    #[Route('/{id<\d+>}/edit', name: "admin.properties.edit", methods: ["GET", "POST"])]
    /**
     * @param Request $request
     * @param Properties $propertie
     * @return Response
     */
    public function edit(Request $request, Properties $propertie): Response
    {
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
        $form = $this->createForm(PropertiesType::class, $propertie, ['propertie' => $propertie]);
        $form->handleRequest($request);

        if ($form->isSubmitted()) {
            $this->addFlash("success", 'Appartement modifiée avec succès.');

            return $this->handle->handlePropertieForm($form);
        }

        return $this->render('admin/properties/properties/edit.html.twig', [
            'propertie' => $propertie,
            'form' => $form->createView(),
            'title' => 'Editer l\'appartement',
            'libelle_ajouter' => 'Nouvel appartement',
            'libelle_liste' => 'Liste des appartements',
            'current_page' => 'properties',
            'current_global' => 'properties'
        ]);
    }

    #[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"
            );
            $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'));
    }
}
