<?php

namespace App\Tests\Entity;

use App\Entity\FacturePrestataire;
use App\Entity\Prestataire;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\File\File;

class FacturePrestataireTest extends TestCase
{
    private function createPrestataire(): Prestataire
    {
        return new Prestataire('Test Prestataire SARL');
    }

    public function testConstructor(): void
    {
        $p = $this->createPrestataire();
        $f = new FacturePrestataire($p, 'FACT-2026-001', 2026, 4);

        $this->assertNull($f->getId());
        $this->assertSame($p, $f->getPrestataire());
        $this->assertSame('FACT-2026-001', $f->getNumFacture());
        $this->assertSame(2026, $f->getYear());
        $this->assertSame(4, $f->getMonth());
        $this->assertSame('0.00', $f->getMontantHt());
        $this->assertSame('0.00', $f->getMontantTtc());
        $this->assertFalse($f->isPaid());
        $this->assertNull($f->getPaidAt());
        $this->assertNull($f->getFacturePdf());
        $this->assertNull($f->getFacturePdfFile());
        $this->assertNull($f->getUpdatedAt());
        $this->assertInstanceOf(\DateTimeImmutable::class, $f->getCreatedAt());
    }

    public function testNumFacture(): void
    {
        $f = new FacturePrestataire($this->createPrestataire(), 'OLD-001', 2026, 1);
        $f->setNumFacture('NEW-002');
        $this->assertSame('NEW-002', $f->getNumFacture());
    }

    public function testMontants(): void
    {
        $f = new FacturePrestataire($this->createPrestataire(), 'FACT-001', 2026, 3);

        $f->setMontantHt('1500.00');
        $this->assertSame('1500.00', $f->getMontantHt());

        $f->setMontantTtc('1800.00');
        $this->assertSame('1800.00', $f->getMontantTtc());
    }

    public function testYearAndMonth(): void
    {
        $f = new FacturePrestataire($this->createPrestataire(), 'FACT-001', 2025, 12);
        $f->setYear(2026);
        $this->assertSame(2026, $f->getYear());
        $f->setMonth(1);
        $this->assertSame(1, $f->getMonth());
    }

    public function testIsPaid(): void
    {
        $f = new FacturePrestataire($this->createPrestataire(), 'FACT-001', 2026, 4);
        $this->assertFalse($f->isPaid());

        $f->setIsPaid(true);
        $this->assertTrue($f->isPaid());

        $f->setIsPaid(false);
        $this->assertFalse($f->isPaid());
    }

    public function testPaidAt(): void
    {
        $f = new FacturePrestataire($this->createPrestataire(), 'FACT-001', 2026, 4);
        $this->assertNull($f->getPaidAt());

        $now = new \DateTimeImmutable();
        $f->setPaidAt($now);
        $this->assertSame($now, $f->getPaidAt());

        $f->setPaidAt(null);
        $this->assertNull($f->getPaidAt());
    }

    public function testFacturePdf(): void
    {
        $f = new FacturePrestataire($this->createPrestataire(), 'FACT-001', 2026, 4);
        $this->assertNull($f->getFacturePdf());

        $f->setFacturePdf('facture-2026-001.pdf');
        $this->assertSame('facture-2026-001.pdf', $f->getFacturePdf());

        $f->setFacturePdf(null);
        $this->assertNull($f->getFacturePdf());
    }

    public function testSetFacturePdfFileTriggersUpdatedAt(): void
    {
        $f = new FacturePrestataire($this->createPrestataire(), 'FACT-001', 2026, 4);
        $this->assertNull($f->getUpdatedAt());

        $file = $this->createStub(File::class);
        $f->setFacturePdfFile($file);

        $this->assertSame($file, $f->getFacturePdfFile());
        $this->assertInstanceOf(\DateTimeImmutable::class, $f->getUpdatedAt());
    }

    public function testSetFacturePdfFileNullDoesNotSetUpdatedAt(): void
    {
        $f = new FacturePrestataire($this->createPrestataire(), 'FACT-001', 2026, 4);
        $f->setFacturePdfFile(null);
        $this->assertNull($f->getUpdatedAt());
        $this->assertNull($f->getFacturePdfFile());
    }

    public function testGetPeriodLabelKnownMonths(): void
    {
        $months = [
            1 => 'Janvier', 2 => 'Fevrier', 3 => 'Mars', 4 => 'Avril',
            5 => 'Mai', 6 => 'Juin', 7 => 'Juillet', 8 => 'Aout',
            9 => 'Septembre', 10 => 'Octobre', 11 => 'Novembre', 12 => 'Decembre',
        ];

        foreach ($months as $num => $name) {
            $f = new FacturePrestataire($this->createPrestataire(), 'F', 2026, $num);
            $this->assertSame($name.' 2026', $f->getPeriodLabel());
        }
    }

    public function testGetPeriodLabelUnknownMonth(): void
    {
        $f = new FacturePrestataire($this->createPrestataire(), 'F', 2026, 13);
        $this->assertSame('13 2026', $f->getPeriodLabel());
    }
}
