<?php

declare(strict_types=1);

namespace App\Controller\Admin\Prints;

use App\Entity\Quittances\Quittances;
use App\Entity\Properties\Properties;
use App\Repository\Properties\ChargescautionsRepository;
use App\Services\Pdf\PdfServices;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security as Security;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;

#[Route('/admin/print/generate')]
#[Security("is_granted('ROLE_SUPER_ADMIN') or is_granted('ROLE_ADMIN')")]
class AdminPrintController extends AbstractController
{
    public function __construct(
        private ChargescautionsRepository $chargescautionsRepository
    ) {}

    #[Route('/propertie/facture/{id<\d+>}', name: "admin.propertie.facture.generate", methods: ['GET'])]
    /**
     * @param Properties $propertie
     * @param Request $request
     * @param PdfServices $pdfServices
     * @return Response
     * @throws LoaderError
     * @throws RuntimeError
     * @throws SyntaxError
     */
    public function generate_facture_caution(
        Properties $propertie,
        Request $request,
        PdfServices $pdfServices
    ): Response {

        $locataire = $propertie->getLocataires();
        $title = 'Facture de ' . $locataire->getName();
        $chargescautions = $this->chargescautionsRepository->findBy(['properties' => $propertie]);

        $charges = $propertie->getCharges(); // récupère toutes les charges de la propriété




        /** @var array<string> $datas */
        $datas = [
            'title' => $title,
            'propertie' => $propertie,
            'chargescautions' => $chargescautions,
            'locataire' => $locataire,
        ];

        $template = 'pdf/propertie.html.twig';

        return $pdfServices->generate(
            $title,
            'A4',
            'portrait',
            $template,
            $datas
        );
    }

    #[Route('/quittance/quittance/{id<\d+>}', name: "admin.quittance.loyer.generate", methods: ['GET'])]
    /**
     * @param Quittances $quittance
     * @param Request $request
     * @param PdfServices $pdfServices
     * @return Response
     * @throws LoaderError
     * @throws RuntimeError
     * @throws SyntaxError
     */
    public function generate_quittance(
        Quittances $quittance,
        Request $request,
        PdfServices $pdfServices
    ): Response {

        $locataire = $quittance->getLocataires();
        $title = 'Quittance N° ' . str_replace('/', '-', $quittance->getCode());

        /** @var array<string> $datas */
        $datas = [
            'title' => $title,
            'quittance' => $quittance,
            'locataire' => $locataire,
        ];

        $template = 'pdf/quittance.html.twig';

        return $pdfServices->generate(
            $title,
            'A4',
            'portrait',
            $template,
            $datas
        );
    }
}
