<?php

namespace App\Service\Pdf;

use App\Entity\Contrats;
use App\Entity\ContratsPayments;
use App\Entity\Devis;
use Fpdf\Fpdf;
use Symfony\Component\HttpKernel\KernelInterface;

class PlPdf extends Fpdf
{
    private ContratsPayments $contratsPayments;
    private string $logo;
    private bool $isExtraPage = false;
    private Contrats $contrat;

    public function __construct(KernelInterface $kernel, ContratsPayments $contratsPayments,Contrats $contrats, $orientation = 'P', $unit = 'mm', $size = 'A4')
    {
        parent::__construct($orientation, $unit, $size);
        $this->contratsPayments = $contratsPayments;
        $this->contrat = $contrats;
        $this->logo = $kernel->getProjectDir()."/public/provider/images/favicon.png";

        $this->AliasNbPages();
        $this->SetAutoPageBreak(true, 35);
    }

    /**
     * Convertit l'UTF-8 en Windows-1252 pour FPDF et gère l'Euro
     */
    private function clean(?string $text): string
    {
        if (!$text) return '';
        $text = iconv('UTF-8', 'windows-1252//TRANSLIT//IGNORE', $text);
        return str_replace('€', chr(128), $text);
    }

    /**
     * Helper pour afficher le symbole Euro proprement
     */
    private function euro(): string
    {
        return ' ' . chr(128);
    }

    public function Header()
    {
        if ($this->page > 0 && !$this->isExtraPage) {
            // --- LOGO ET INFOS SOCIÉTÉ ---
            $this->SetY(10);
            if (file_exists($this->logo)) {
                $this->Image($this->logo, 10, 10, 15); // Un peu plus grand pour le reçu
                $this->SetX(28);
            }

            $this->SetFont('Arial', 'B', 14);
            $this->SetTextColor(37, 99, 235); // Bleu Ludikevent
            $this->Cell(100, 7, $this->clean('Lilian SEGARD - Ludik Event'), 0, 0, 'L');

            // BADGE "REÇU" À DROITE
            $this->SetFont('Arial', 'B', 11);
            $this->SetFillColor(37, 99, 235);
            $this->SetTextColor(255, 255, 255);
            $this->SetX(150);
            $this->Cell(50, 8, $this->clean('REÇU DE PAIEMENT'), 0, 1, 'C', true);

            // COORDONNÉES SOUS LE NOM
            $this->SetY(17);
            $this->SetX(28);
            $this->SetFont('Arial', '', 8);
            $this->SetTextColor(80, 80, 80);
            $this->Cell(0, 4, $this->clean('SIRET : 930 488 408 00012 | 6 Rue du Château, 02800 Danizy'), 0, 1, 'L');
            $this->SetX(28);
            $this->Cell(0, 4, $this->clean('Tél. : 06 14 17 24 47 | contact@ludikevent.fr'), 0, 1, 'L');

            $this->Ln(12);

            // --- TITRE DU DOCUMENT ET RÉFÉRENCE ---
            $this->SetFont('Arial', 'B', 15);
            $this->SetTextColor(17, 24, 39); // Gris très foncé/Noir
            $this->Cell(0, 10, $this->clean('Confirmation de règlement'), 0, 1, 'L');

            $this->SetFont('Arial', 'B', 10);
            $this->SetTextColor(107, 114, 128); // Gris intermédiaire
            $this->Cell(0, 5, $this->clean('Référence Réservation : #' . $this->contrat->getNumReservation()), 0, 1, 'L');

            // LIGNE DE SÉPARATION DESIGN
            $this->Ln(2);
            $this->SetDrawColor(37, 99, 235);
            $this->SetLineWidth(0.8);
            $this->Line(10, $this->GetY(), 40, $this->GetY()); // Petite ligne bleue
            $this->SetDrawColor(229, 231, 235); // Gris clair
            $this->SetLineWidth(0.2);
            $this->Line(40, $this->GetY(), 200, $this->GetY()); // Ligne grise qui continue

            $this->Ln(5);
        }
    }
    public function generate(): string
    {
        $this->AddPage();
        $this->renderContent();
        return $this->Output('S');
    }

    private function renderContent(): void
    {
        // --- 1. BLOC INFO PAIEMENT (Le "Reçu") ---
        $this->SetY(55);
        $this->SetFont('Arial', 'B', 11);
        $this->SetFillColor(245, 245, 255); // Bleu très léger
        $this->SetTextColor(37, 99, 235);
        $this->Cell(0, 10, $this->clean(" DÉTAILS DU RÈGLEMENT"), 0, 1, 'L', true);

        $this->Ln(4);
        $this->SetFont('Arial', '', 10);
        $this->SetTextColor(50, 50, 50);

        // Tableau à deux colonnes pour les infos
        $startY = $this->GetY();

        $this->SetFont('Arial', 'B', 10);
        $this->Cell(50, 8, $this->clean("Montant réglé :"), 0, 0);
        $this->SetFont('Arial', 'B', 12); $this->SetTextColor(22, 163, 74); // Vert succès
        $this->Cell(0, 8, number_format($this->contratsPayments->getAmount(), 2) . $this->euro(), 0, 1);

        $this->SetTextColor(50, 50, 50);
        $this->SetFont('Arial', 'B', 10);
        $this->Cell(50, 8, $this->clean("Date du paiement :"), 0, 0);
        $this->SetFont('Arial', '', 10);
        $this->Cell(0, 8, $this->contratsPayments->getValidateAt()->format('d/m/Y H:i'), 0, 1);

        $this->SetFont('Arial', 'B', 10);
        $this->Cell(50, 8, $this->clean("Mode de règlement :"), 0, 0);
        $this->SetFont('Arial', '', 10);
        if($this->contratsPayments->getCard()['type'] == "manuel") {
            $this->Cell(0, 8, $this->clean("Paiement valider par Ludikevent"), 0, 1);
        } else {
            $this->Cell(0, 8, $this->clean($this->contratsPayments->getCard()['type'] ?? 'Carte Bancaire'), 0, 1);
        }


        $this->SetFont('Arial', 'B', 10);
        $this->Cell(50, 8, $this->clean("Référence transaction :"), 0, 0);
        $this->SetFont('Arial', 'I', 9);
        $this->Cell(0, 8, $this->clean($this->contratsPayments->getPaymentId() ?? 'N/A'), 0, 1);

        $this->Ln(10);

        // --- 2. BLOC CLIENT ---
        $this->SetFont('Arial', 'B', 11);
        $this->SetFillColor(250, 250, 250);
        $this->SetTextColor(0, 0, 0);
        $this->Cell(0, 10, $this->clean(" INFORMATIONS CLIENT"), 0, 1, 'L', true);

        $this->Ln(2);
        $this->SetFont('Arial', '', 10);
        $customer = $this->contrat->getCustomer();
        $this->MultiCell(0, 6, $this->clean(
            $customer->getSurname() . " " . $customer->getName() . "\n" .
            $customer->getEmail() . "\n" .
            "Réservation N° " . $this->contrat->getNumReservation()
        ), 0, 'L');

        $this->Ln(15);

        // --- 3. MESSAGE DE CONFIRMATION ---
        $this->SetDrawColor(22, 163, 74);
        $this->SetFillColor(240, 253, 244);
        $this->SetLineWidth(0.2);
        $this->SetFont('Arial', 'B', 10);
        $this->SetTextColor(21, 128, 61);

        $text = "Ce document confirme que votre paiement a bien été reçu et validé par Ludik Event.\nNous vous remercions de votre confiance.";
        $this->MultiCell(0, 10, $this->clean($text), 1, 'C', true);

        // --- 4. SIGNATURE / CACHET ---
        $this->SetY(-60);
        $this->SetFont('Arial', 'B', 9);
        $this->SetTextColor(0, 0, 0);
        $this->Cell(0, 5, $this->clean('{{Sign;type=signature;role=Ludikevent;height=120;width=236}}'), 0, 1, 'C');
    }
    public function Footer(): void
    {
        // Positionnement à 1,5 cm du bas
        $this->SetY(-15);
        $this->SetFont('Arial', 'I', 7);
        $this->SetTextColor(150, 150, 150);

        // Texte gauche : Identification du devis
        $this->Cell(0, 10, $this->clean('Confirmation de paiement Ludik Event - Lilian SEGARD - SIRET 93048840800012'), 0, 0, 'L');


        // Numérotation des pages à droite
        $this->SetTextColor(150, 150, 150);
        $this->SetFont('Arial', 'I', 8);
        $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'R');
    }
}
