<?php

namespace App\Service\Pdf;

use setasign\Fpdi\Fpdi;
use Symfony\Component\HttpKernel\KernelInterface;

if (!\defined('EURO')) {
    \define('EURO', \chr(128)); // @codeCoverageIgnore
}

class RapportFinancierPdf extends Fpdi
{
    private string $periodFrom;
    private string $periodTo;

    /** @var array<string, float> */
    private array $recettes = [];

    /** @var array<string, float> */
    private array $depenses = [];

    private float $totalRecettes = 0.0;
    private float $totalDepenses = 0.0;

    public function __construct(
        private readonly KernelInterface $kernel,
        string $periodFrom,
        string $periodTo,
    ) {
        parent::__construct();
        $this->periodFrom = $periodFrom;
        $this->periodTo = $periodTo;

        $this->SetTitle($this->enc('Rapport financier - '.$periodFrom.' au '.$periodTo));
        $this->SetAuthor($this->enc('Association E-Cosplay'));
        $this->SetCreator('CRM E-Cosplay');
    }

    /**
     * @param array<string, float> $recettes Libelle => montant
     * @param array<string, float> $depenses Libelle => montant
     */
    public function setData(array $recettes, array $depenses): void
    {
        $this->recettes = $recettes;
        $this->depenses = $depenses;
        $this->totalRecettes = array_sum($recettes);
        $this->totalDepenses = array_sum($depenses);
    }

    public function generate(): void
    {
        $this->AliasNbPages();
        $this->AddPage();

        $this->writeContextBlock();
        $this->writeRecettes();
        $this->writeDepenses();
        $this->writeBilan();
        $this->writeSignatureBlock();
    }

    // ---------------------------------------------------------------
    // Header / Footer
    // ---------------------------------------------------------------

    public function Header(): void
    {
        $logo = $this->kernel->getProjectDir().'/public/logo.jpg';
        if (file_exists($logo)) {
            $this->Image($logo, 10, 8, 45);
        }

        $this->SetFont('Arial', 'B', 18);
        $this->SetXY(60, 10);
        $this->Cell(0, 8, $this->enc('RAPPORT FINANCIER'), 0, 1, 'L');

        $this->SetFont('Arial', '', 11);
        $this->SetXY(60, 19);
        $this->Cell(0, 5, $this->enc('Periode : du '.$this->periodFrom.' au '.$this->periodTo), 0, 1, 'L');

        $formatter = new \IntlDateFormatter(
            'fr_FR',
            \IntlDateFormatter::FULL,
            \IntlDateFormatter::NONE,
            'Europe/Paris',
            \IntlDateFormatter::GREGORIAN
        );
        $this->SetFont('Arial', '', 9);
        $this->SetTextColor(120, 120, 120);
        $this->SetXY(60, 25);
        $this->Cell(0, 5, $this->enc('Edite le '.$formatter->format(new \DateTime())), 0, 1, 'L');
        $this->SetTextColor(0, 0, 0);

        // Mention document public
        $this->SetFont('Arial', 'I', 8);
        $this->SetTextColor(100, 100, 100);
        $this->SetXY(10, 35);
        $this->Cell(0, 4, $this->enc('Document public - Association loi 1901 - Les montants sont presentes de maniere synthetique.'), 0, 1, 'C');
        $this->SetTextColor(0, 0, 0);

        $this->Ln(5);
    }

    public function Footer(): void
    {
        $this->SetY(-22);
        $this->SetDrawColor(253, 140, 4);
        $this->Line(15, $this->GetY(), 195, $this->GetY());
        $this->Ln(3);
        $this->SetFont('Arial', '', 8);
        $this->SetTextColor(0, 0, 0);
        $this->Cell(190, 4, $this->enc('42 rue de Saint-Quentin - 02800 BEAUTOR - Tel: 07 66 95 70 06 - client@e-cosplay.fr'), 0, 1, 'C');
        $this->Cell(190, 4, $this->enc('Association E-Cosplay - N SIRET 943 121 517 00011 - CODE APE 9329Z - RNA W022006988'), 0, 1, 'C');

        $this->SetFont('Arial', 'I', 7);
        $this->SetTextColor(150, 150, 150);
        $this->Cell(190, 4, $this->enc('Page ').$this->PageNo().' / {nb}', 0, 0, 'C');
    }

    // ---------------------------------------------------------------
    // Bloc legal
    // ---------------------------------------------------------------

    private function writeContextBlock(): void
    {
        $this->SetY(45);

        $this->SetDrawColor(200, 200, 200);
        $this->Cell(0, 0.5, '', 'T', 1, 'L');
        $this->Ln(2);

        $this->SetFont('Arial', 'B', 11);
        $this->Cell(0, 6, $this->enc('INFORMATIONS LEGALES'), 0, 1, 'C');
        $this->Ln(1);

        $labelW = 55;
        $this->SetFont('Arial', '', 9);

        $this->Cell($labelW, 5, $this->enc('Association :'), 0, 0, 'L');
        $this->SetFont('Arial', 'B', 9);
        $this->Cell(0, 5, $this->enc('E-Cosplay - Association loi 1901 - RNA W022006988'), 0, 1, 'L');
        $this->SetFont('Arial', '', 9);

        $this->Cell($labelW, 5, $this->enc('Siege social :'), 0, 0, 'L');
        $this->Cell(0, 5, $this->enc('42 rue de Saint-Quentin, 02800 Beautor'), 0, 1, 'L');

        $this->Cell($labelW, 5, $this->enc('SIRET :'), 0, 0, 'L');
        $this->Cell(0, 5, $this->enc('943 121 517 00011'), 0, 1, 'L');

        $this->Cell($labelW, 5, $this->enc('Activite :'), 0, 0, 'L');
        $this->Cell(0, 5, $this->enc('Services numeriques (hebergement, noms de domaine, messagerie)'), 0, 1, 'L');

        $this->Ln(2);
        $this->Cell(0, 0.5, '', 'T', 1, 'L');
        $this->Ln(5);
    }

    // ---------------------------------------------------------------
    // Recettes (montant entrant)
    // ---------------------------------------------------------------

    private function writeRecettes(): void
    {
        $this->SetFont('Arial', 'B', 12);
        $this->SetTextColor(22, 163, 74);
        $this->Cell(0, 7, $this->enc('RECETTES (MONTANTS ENTRANTS)'), 0, 1, 'L');
        $this->SetTextColor(0, 0, 0);
        $this->Ln(2);

        // En-tete
        $this->SetFont('Arial', 'B', 9);
        $this->SetFillColor(35, 35, 35);
        $this->SetTextColor(255, 255, 255);
        $this->Cell(120, 7, $this->enc('  Service'), 1, 0, 'L', true);
        $this->Cell(50, 7, $this->enc('Montant HT'), 1, 1, 'R', true);
        $this->SetTextColor(0, 0, 0);

        // Lignes
        $this->SetFont('Arial', '', 10);
        $fill = false;
        foreach ($this->recettes as $label => $montant) {
            $this->SetFillColor(245, 245, 240);
            $this->Cell(120, 7, $this->enc('  '.$label), 'B', 0, 'L', $fill);
            $this->Cell(50, 7, number_format($montant, 2, ',', ' ').' '.EURO, 'B', 1, 'R', $fill);
            $fill = !$fill;
        }

        // Total
        $this->SetFont('Arial', 'B', 11);
        $this->SetFillColor(22, 163, 74);
        $this->SetTextColor(255, 255, 255);
        $this->Cell(120, 8, $this->enc('  TOTAL RECETTES'), 0, 0, 'L', true);
        $this->Cell(50, 8, number_format($this->totalRecettes, 2, ',', ' ').' '.EURO, 0, 1, 'R', true);
        $this->SetTextColor(0, 0, 0);

        $this->Ln(8);
    }

    // ---------------------------------------------------------------
    // Depenses (montant sortant)
    // ---------------------------------------------------------------

    private function writeDepenses(): void
    {
        $this->SetFont('Arial', 'B', 12);
        $this->SetTextColor(220, 38, 38);
        $this->Cell(0, 7, $this->enc('DEPENSES (MONTANTS SORTANTS)'), 0, 1, 'L');
        $this->SetTextColor(0, 0, 0);
        $this->Ln(2);

        // En-tete
        $this->SetFont('Arial', 'B', 9);
        $this->SetFillColor(35, 35, 35);
        $this->SetTextColor(255, 255, 255);
        $this->Cell(120, 7, $this->enc('  Poste de depense'), 1, 0, 'L', true);
        $this->Cell(50, 7, $this->enc('Montant'), 1, 1, 'R', true);
        $this->SetTextColor(0, 0, 0);

        // Lignes
        $this->SetFont('Arial', '', 10);
        $fill = false;
        foreach ($this->depenses as $label => $montant) {
            $this->SetFillColor(245, 245, 240);
            $this->Cell(120, 7, $this->enc('  '.$label), 'B', 0, 'L', $fill);
            $this->Cell(50, 7, number_format($montant, 2, ',', ' ').' '.EURO, 'B', 1, 'R', $fill);
            $fill = !$fill;
        }

        // Total
        $this->SetFont('Arial', 'B', 11);
        $this->SetFillColor(220, 38, 38);
        $this->SetTextColor(255, 255, 255);
        $this->Cell(120, 8, $this->enc('  TOTAL DEPENSES'), 0, 0, 'L', true);
        $this->Cell(50, 8, number_format($this->totalDepenses, 2, ',', ' ').' '.EURO, 0, 1, 'R', true);
        $this->SetTextColor(0, 0, 0);

        $this->Ln(8);
    }

    // ---------------------------------------------------------------
    // Bilan
    // ---------------------------------------------------------------

    private function writeBilan(): void
    {
        $marge = $this->totalRecettes - $this->totalDepenses;
        $isPositif = $marge >= 0;

        $this->SetDrawColor(200, 200, 200);
        $this->Cell(0, 0.5, '', 'T', 1, 'L');
        $this->Ln(3);

        $this->SetFont('Arial', 'B', 13);
        $this->Cell(0, 7, $this->enc('BILAN'), 0, 1, 'C');
        $this->Ln(2);

        // Tableau bilan
        $this->SetFont('Arial', '', 11);
        $col1 = 90;
        $col2 = 80;

        $this->SetX(15);
        $this->Cell($col1, 8, $this->enc('Total des recettes :'), 0, 0, 'L');
        $this->SetFont('Arial', 'B', 11);
        $this->SetTextColor(22, 163, 74);
        $this->Cell($col2, 8, '+ '.number_format($this->totalRecettes, 2, ',', ' ').' '.EURO, 0, 1, 'R');
        $this->SetTextColor(0, 0, 0);

        $this->SetFont('Arial', '', 11);
        $this->SetX(15);
        $this->Cell($col1, 8, $this->enc('Total des depenses :'), 0, 0, 'L');
        $this->SetFont('Arial', 'B', 11);
        $this->SetTextColor(220, 38, 38);
        $this->Cell($col2, 8, '- '.number_format($this->totalDepenses, 2, ',', ' ').' '.EURO, 0, 1, 'R');
        $this->SetTextColor(0, 0, 0);

        $this->Ln(1);
        $this->SetDrawColor(0, 0, 0);
        $this->Line(15, $this->GetY(), 185, $this->GetY());
        $this->Ln(2);

        // Resultat
        $this->SetFont('Arial', 'B', 14);
        $this->SetX(15);
        $this->Cell($col1, 10, $this->enc('Resultat net :'), 0, 0, 'L');
        $this->SetTextColor($isPositif ? 22 : 220, $isPositif ? 163 : 38, $isPositif ? 74 : 38);
        $sign = $isPositif ? '+ ' : '- ';
        $this->Cell($col2, 10, $sign.number_format(abs($marge), 2, ',', ' ').' '.EURO, 0, 1, 'R');
        $this->SetTextColor(0, 0, 0);

        // Statut
        $this->Ln(3);
        $this->SetFont('Arial', 'B', 12);
        if ($isPositif) {
            $this->SetFillColor(22, 163, 74);
            $this->SetTextColor(255, 255, 255);
            $label = $marge > $this->totalRecettes * 0.3 ? 'EXCEDENT' : 'EQUILIBRE';
        } else {
            $this->SetFillColor(220, 38, 38);
            $this->SetTextColor(255, 255, 255);
            $label = 'DEFICIT';
        }
        $this->Cell(0, 10, $this->enc($label), 0, 1, 'C', true);
        $this->SetTextColor(0, 0, 0);

        $this->Ln(5);
    }

    // ---------------------------------------------------------------
    // Signature
    // ---------------------------------------------------------------

    private function writeSignatureBlock(): void
    {
        if ($this->GetY() + 40 > $this->GetPageHeight() - 25) {
            $this->AddPage();
        }

        $this->SetDrawColor(200, 200, 200);
        $this->Cell(0, 0.5, '', 'T', 1, 'L');
        $this->Ln(3);

        $formatter = new \IntlDateFormatter(
            'fr_FR',
            \IntlDateFormatter::LONG,
            \IntlDateFormatter::NONE,
            'Europe/Paris',
            \IntlDateFormatter::GREGORIAN
        );

        $this->SetFont('Arial', '', 9);
        $this->Cell(0, 5, $this->enc('Fait a Beautor, le '.$formatter->format(new \DateTime())), 0, 1, 'L');
        $this->Ln(2);

        $this->SetFont('Arial', 'B', 9);
        $this->Cell(0, 5, $this->enc('Signature du responsable :'), 0, 1, 'L');
        $this->Ln(1);

        $this->SetAutoPageBreak(false);
        $this->SetFont('Arial', '', 10);
        $this->SetTextColor(0, 0, 0);
        $this->Cell(60, 20, '{{Sign;type=signature;role=First Party}}', 0, 1, 'L');
    }

    // ---------------------------------------------------------------
    // Helpers
    // ---------------------------------------------------------------

    private function enc(string $text): string
    {
        return mb_convert_encoding($text, 'Windows-1252', 'UTF-8');
    }
}
