<?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');

        $user = $this->getUser();

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

        return $this->render('admin/properties/properties/index.html.twig', [
            //'properties' => $propertiesRepository->getAll(),
            'properties' => $properties,
            '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()) {
            $propertie->setCompanyId($this->getUser()->getCompanyId());
            $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'
        ]);
    }
}
