<?php

namespace App\Service\Pdf;

use App\Entity\Join;
use Doctrine\ORM\EntityManagerInterface;
use Fpdf\Fpdf;
use Symfony\Component\HttpKernel\KernelInterface;

class Candidat extends Fpdf
{
    private Join $join;
    private EntityManagerInterface $em;
    private KernelInterface $kernel;

    public function __construct($orientation = 'P', $unit = 'mm', $size = 'A4')
    {
        parent::__construct($orientation, $unit, $size);
        $this->AliasNbPages();
        $this->SetAutoPageBreak(true, 20);
    }

    public function setData(Join $join, EntityManagerInterface $em, KernelInterface $kernel)
    {
        $this->join = $join;
        $this->em = $em;
        $this->kernel = $kernel;
    }

    public function Header()
    {
        $yPos = 10;
        $xPos = 10;
        $logoWidth = 25;
        $logoHeight = 15;

        $this->SetY($yPos);
        $agDate = $this->join->getCreateAt() ? $this->join->getCreateAt()->format('d/m/Y') : date('d/m/Y');

        $this->SetFont('Arial', 'B', 16);
        $this->SetTextColor(0, 0, 0);
        $this->Cell(0, 7, utf8_decode("FICHE CANDIDAT"), 0, 1, 'C');

        $this->SetFont('Arial', '', 10);
        $this->Cell(0, 6, utf8_decode("Générée le " . $agDate), 0, 1, 'C');

        $logoPath = $this->kernel->getProjectDir() . '/public/assets/images/logo.jpg';
        if (file_exists($logoPath)) {
            $this->Image($logoPath, $xPos, $yPos, $logoWidth, $logoHeight);
        }

        $this->Ln(5);

        if ($this->PageNo() == 1) {
            $this->writeAgContextBlock();
        } else {
            $this->SetY(30);
        }
    }

    public function writeAgContextBlock()
    {
        $this->SetFont('Arial', 'B', 9);
        $this->Cell(0, 1, '', 'T', 1, 'L');
        $this->Ln(2);
        $this->Cell(0, 5, utf8_decode('INFORMATIONS LÉGALES'), 0, 1, 'C');
        $this->SetFont('Arial', '', 9);
        $this->Cell(35, 4, utf8_decode('Association :'), 0, 0);
        $this->SetFont('Arial', 'B', 9);
        $this->Cell(0, 4, utf8_decode('E-Cosplay Association loi 1901 – RNA N°W022006988'), 0, 1);
        $this->SetFont('Arial', '', 9);
        $this->Cell(35, 4, utf8_decode('Siège social :'), 0, 0);
        $this->Cell(0, 4, utf8_decode('42 rue de saint-quentin 02800 Beautor'), 0, 1);
        $this->Ln(2);
        $this->Cell(0, 1, '', 'T', 1, 'L');
        $this->Ln(5);
    }

    public function contentDetails()
    {
        // --- SECTION 1 : ÉTAT CIVIL ---
        $this->drawSectionTitle('IDENTITÉ DU CANDIDAT');

        $this->infoRow('Nom / Prénom :', strtoupper($this->join->getSurname()) . ' ' . $this->join->getName(), true);

        // --- CALCUL ET AFFICHAGE DE L'ÂGE ---
        $birthDate = $this->join->getDateBirth();
        $this->SetFont('Arial', '', 10);
        $this->Cell(40, 7, utf8_decode('Date de naissance :'), 0, 0);

        if ($birthDate) {
            $age = $this->calculateAge($birthDate);
            $dateStr = $birthDate->format('d/m/Y');

            // Texte de la date
            $this->SetFont('Arial', 'B', 10);
            $this->Cell(30, 7, $dateStr, 0, 0);

            // Affichage de l'âge avec couleur
            if ($age >= 16) {
                $this->SetTextColor(0, 150, 0); // Vert
            } else {
                $this->SetTextColor(200, 0, 0); // Rouge
            }

            $this->Cell(0, 7, utf8_decode(" (" . $age . " ans)"), 0, 1);
            $this->SetTextColor(0, 0, 0); // Reset noir
        } else {
            $this->Cell(0, 7, 'N/C', 0, 1);
        }

        $genreInfo = sprintf("Sexe : %s | Pronom : %s",
            $this->join->getSexe() ?? 'N/C',
            $this->join->getPronom() ?? 'N/C'
        );
        $this->infoRow('Genre :', $genreInfo);
        $this->Ln(3);

        // --- SECTION 2 : CONTACT ---
        $this->drawSectionTitle('COORDONNÉES');
        $this->infoRow('Email :', $this->join->getEmail(), true);
        $this->infoRow('Téléphone :', $this->join->getPhone());
        $address = sprintf("%s, %s %s", $this->join->getAddress(), $this->join->getZipCode(), $this->join->getCity());
        $this->infoRow('Adresse :', $address);
        $this->Ln(3);

        // --- SECTION 3 : PROFIL ---
        $this->drawSectionTitle('PROFIL ET RÔLES');
        $roles = $this->join->getRole();
        $this->infoRow('Rôles visés :', is_array($roles) ? implode(', ', $roles) : ($roles ?? 'Aucun'), true);

        $this->Ln(2);
        $this->SetFont('Arial', 'B', 10);
        $this->Cell(0, 6, utf8_decode("Présentation / Qui est-ce ?"), 0, 1);
        $this->SetFont('Arial', '', 10);
        $this->MultiCell(0, 5, utf8_decode($this->join->getWho() ?? "Aucune description fournie."), 1, 'L');
        $this->Ln(5);

        // --- SECTION 4 : RÉSEAUX SOCIAUX ---
        $this->drawSectionTitle('RÉSEAUX SOCIAUX');
        $this->infoRow('Discord :', $this->join->getDiscordAccount() ?? 'Non renseigné');
        $this->infoRow('Instagram :', $this->join->getInstaLink() ?? 'N/A');
        $this->infoRow('TikTok :', $this->join->getTiktokLink() ?? 'N/A');
        $this->infoRow('Facebook :', $this->join->getFacebookLink() ?? 'N/A');
    }

    private function calculateAge(\DateTimeInterface $birthDate): int
    {
        $now = new \DateTime();
        return $now->diff($birthDate)->y;
    }

    private function infoRow($label, $value, $important = false)
    {
        $this->SetFont('Arial', '', 10);
        $this->Cell(40, 7, utf8_decode($label), 0, 0);
        if ($important) $this->SetFont('Arial', 'B', 10);
        $this->MultiCell(0, 7, utf8_decode($value), 0, 'L');
    }

    private function drawSectionTitle($title)
    {
        $this->SetFillColor(240, 240, 240);
        $this->SetFont('Arial', 'B', 11);
        $this->Cell(0, 8, utf8_decode('  ' . $title), 0, 1, 'L', true);
        $this->Ln(2);
    }

    public function Footer()
    {
        $this->SetY(-15);
        $this->SetFont('Arial', 'I', 8);
        $this->SetTextColor(128, 128, 128);
        $this->Cell(0, 10, 'Fiche Candidat E-Cosplay - Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
    }
}
