<?php

namespace App\Tests\Service\Pdf;

use App\Entity\Customer;
use App\Entity\Devis;
use App\Repository\ProductRepository;
use App\Service\Pdf\DevisPdfService;
use Doctrine\Common\Collections\ArrayCollection;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpKernel\KernelInterface;

class DevisPdfServiceTest 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);

        $devis = $this->createStub(Devis::class);
        $productRepository = $this->createStub(ProductRepository::class);
        $customer = $this->createStub(Customer::class);

        $devis->method('getNum')->willReturn('DEV-001');
        $devis->method('getCustomer')->willReturn($customer);
        $devis->method('getBillAddress')->willReturn(null); // Simplify
        $devis->method('getAddressShip')->willReturn(null);
        $devis->method('getStartAt')->willReturn(new \DateTimeImmutable('2023-01-01'));
        $devis->method('getEndAt')->willReturn(new \DateTimeImmutable('2023-01-02'));
        $devis->method('getDevisLines')->willReturn(new ArrayCollection([]));
        $devis->method('getDevisOptions')->willReturn(new ArrayCollection([]));

        $customer->method('getName')->willReturn('Client SAS');
        $customer->method('getEmail')->willReturn('client@example.com');

        $pdfService = new DevisPdfService($kernel, $devis, $productRepository);
        $content = $pdfService->generate();

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