<?php

namespace App\Tests\Service\Pdf;

use App\Entity\Devis;
use App\Entity\DevisLine;
use App\Entity\OrderNumber;
use App\Service\Pdf\DevisPdf;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpKernel\KernelInterface;

class DevisPdfTest extends TestCase
{
    private string $projectDir;
    private KernelInterface $kernel;

    protected function setUp(): void
    {
        $this->projectDir = sys_get_temp_dir().'/devis_pdf_test_'.bin2hex(random_bytes(4));
        mkdir($this->projectDir.'/public', 0777, true);

        $this->kernel = $this->createStub(KernelInterface::class);
        $this->kernel->method('getProjectDir')->willReturn($this->projectDir);
    }

    protected function tearDown(): void
    {
        @unlink($this->projectDir.'/public/logo.jpg');
        @rmdir($this->projectDir.'/public');
        @rmdir($this->projectDir);
    }

    private function makeDevis(string $num = '04/2026-00001'): Devis
    {
        $on = new OrderNumber($num);
        $devis = new Devis($on, 'test-secret');
        $devis->setTotalHt('100.00');
        $devis->setTotalTva('0.00');
        $devis->setTotalTtc('100.00');

        return $devis;
    }

    public function testGenerateEmptyDevis(): void
    {
        $devis = $this->makeDevis();
        $pdf = new DevisPdf($this->kernel, $devis);
        $pdf->generate();
        $output = $pdf->Output('S');
        $this->assertStringStartsWith('%PDF', $output);
    }

    public function testGenerateWithLines(): void
    {
        $devis = $this->makeDevis();
        $line = new DevisLine($devis, 'Service Web', '80.00', 0);
        $line->setDescription('Creation site vitrine');
        $devis->addLine($line);

        $pdf = new DevisPdf($this->kernel, $devis);
        $pdf->generate();
        $output = $pdf->Output('S');
        $this->assertStringStartsWith('%PDF', $output);
    }

    public function testGenerateWithTva(): void
    {
        $devis = $this->makeDevis();
        $devis->setTotalHt('100.00');
        $devis->setTotalTva('20.00');
        $devis->setTotalTtc('120.00');

        $line = new DevisLine($devis, 'Service', '100.00', 0);
        $devis->addLine($line);

        $pdf = new DevisPdf($this->kernel, $devis);
        $pdf->generate();
        $output = $pdf->Output('S');
        $this->assertStringStartsWith('%PDF', $output);
    }

    public function testGenerateWithLogo(): void
    {
        // Create a minimal JPEG
        $img = imagecreatetruecolor(10, 10);
        imagejpeg($img, $this->projectDir.'/public/logo.jpg');
        imagedestroy($img);

        $devis = $this->makeDevis();
        $pdf = new DevisPdf($this->kernel, $devis);
        $pdf->generate();
        $output = $pdf->Output('S');
        $this->assertStringStartsWith('%PDF', $output);
    }

    public function testEncMethod(): void
    {
        $devis = $this->makeDevis();
        $pdf = new DevisPdf($this->kernel, $devis);

        $ref = new \ReflectionMethod(DevisPdf::class, 'enc');
        $ref->setAccessible(true);

        $this->assertSame('test', $ref->invoke($pdf, 'test'));
    }
}
