<?php

namespace App\Tests\Service\Pdf;

use App\Entity\Contrats;
use App\Entity\Customer;
use App\Service\Pdf\ContratPdfService;
use Doctrine\Common\Collections\ArrayCollection;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpKernel\KernelInterface;

class ContratPdfServiceTest extends TestCase
{
    public function testGenerate(): void
    {
        $kernel = $this->createStub(KernelInterface::class);
        $kernel->method('getProjectDir')->willReturn(sys_get_temp_dir());

        // Create dummy favicon for logo check
        $faviconDir = sys_get_temp_dir() . '/public/provider/images';
        if (!is_dir($faviconDir)) {
            mkdir($faviconDir, 0777, true);
        }
        $pngData = base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==');
        file_put_contents($faviconDir . '/favicon.png', $pngData);

        $contrat = $this->createStub(Contrats::class);
        $customer = $this->createStub(Customer::class);

        $contrat->method('getNumReservation')->willReturn('RES-123');
        $contrat->method('getCustomer')->willReturn($customer);
        $contrat->method('getAddressEvent')->willReturn('123 Event St');
        $contrat->method('getZipCodeEvent')->willReturn('75000');
        $contrat->method('getTownEvent')->willReturn('Paris');
        $contrat->method('getDateAt')->willReturn(new \DateTimeImmutable('2023-01-01'));
        $contrat->method('getEndAt')->willReturn(new \DateTimeImmutable('2023-01-02')); // 2 days
        $contrat->method('getContratsLines')->willReturn(new ArrayCollection([]));
        $contrat->method('getContratsOptions')->willReturn(new ArrayCollection([]));
        
        // Mock nullable strings
        $contrat->method('getTypeSol')->willReturn('Herbe');
        $contrat->method('getDistancePower')->willReturn(10.0);
        $contrat->method('getPente')->willReturn('Plat');
        $contrat->method('getAccess')->willReturn('Facile');
        $contrat->method('getAddress2Event')->willReturn(null);
        $contrat->method('getAddress3Event')->willReturn(null);

        $customer->method('getName')->willReturn('Doe');
        $customer->method('getSurname')->willReturn('John');
        $customer->method('getPhone')->willReturn('0102030405');
        $customer->method('getEmail')->willReturn('john@example.com');

        $pdfService = new ContratPdfService($kernel, $contrat);
        $content = $pdfService->generate();

        $this->assertNotEmpty($content);
        $this->assertStringStartsWith('%PDF', $content);
    }
}
