<?php

namespace Daniv04\HaciendaPackage\Tests\Unit;

use Daniv04\HaciendaPackage\Services\Pdf\PdfGeneratorService;
use Daniv04\HaciendaPackage\Tests\TestCase;

class PdfGeneratorServiceTest extends TestCase
{
    // -------------------------------------------------------------------------
    // Exception when dompdf is not installed
    // -------------------------------------------------------------------------

    public function test_throws_runtime_exception_when_dompdf_not_installed(): void
    {
        if (class_exists(\Barryvdh\DomPDF\Facade\Pdf::class)) {
            $this->markTestSkipped('barryvdh/laravel-dompdf is installed — skipping exception path test.');
        }

        $receipt = $this->makeReceipt();
        $this->makePayload($receipt->id);

        $this->expectException(\RuntimeException::class);
        $this->expectExceptionMessageMatches('/barryvdh\/laravel-dompdf is required/');

        (new PdfGeneratorService())->generate($receipt);
    }

    // -------------------------------------------------------------------------
    // Happy path — only runs when dompdf is installed
    // -------------------------------------------------------------------------

    public function test_generates_pdf_bytes_when_dompdf_installed(): void
    {
        if (!class_exists(\Barryvdh\DomPDF\Facade\Pdf::class)) {
            $this->markTestSkipped('barryvdh/laravel-dompdf is not installed.');
        }

        $receipt = $this->makeReceipt();
        $this->makePayload($receipt->id);

        $bytes = (new PdfGeneratorService())->generate($receipt);

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

    public function test_generates_pdf_with_receiver_data_when_dompdf_installed(): void
    {
        if (!class_exists(\Barryvdh\DomPDF\Facade\Pdf::class)) {
            $this->markTestSkipped('barryvdh/laravel-dompdf is not installed.');
        }

        $receipt = $this->makeReceipt([
            'receipt_type'  => 'NC',
            'receiver_name' => 'Empresa Receptora S.A.',
        ]);
        $this->makePayload($receipt->id);

        $bytes = (new PdfGeneratorService())->generate($receipt);

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

    // -------------------------------------------------------------------------
    // resolveLabel — private method via reflection, no dompdf needed
    // -------------------------------------------------------------------------

    #[\PHPUnit\Framework\Attributes\DataProvider('receiptTypeLabelsProvider')]
    public function test_resolve_label_returns_correct_document_type(string $type, string $expected): void
    {
        $method = new \ReflectionMethod(PdfGeneratorService::class, 'resolveLabel');
        $method->setAccessible(true);

        $this->assertSame($expected, $method->invoke(new PdfGeneratorService(), $type));
    }

    public static function receiptTypeLabelsProvider(): array
    {
        return [
            'FE  → Factura Electrónica'                              => ['FE',  'Factura Electrónica'],
            'FEE → Factura Electrónica de Exportación'               => ['FEE', 'Factura Electrónica de Exportación'],
            'FEC → Factura Electrónica de Compra'                    => ['FEC', 'Factura Electrónica de Compra'],
            'TE  → Tiquete Electrónico'                              => ['TE',  'Tiquete Electrónico'],
            'ND  → Nota de Débito'                                   => ['ND',  'Nota de Débito'],
            'NC  → Nota de Crédito'                                  => ['NC',  'Nota de Crédito'],
            'REP → Recibo Electrónico de Pago'                       => ['REP', 'Recibo Electrónico de Pago'],
            'CA  → Confirmación de Comprobante Electrónico'          => ['CA',  'Confirmación de Comprobante Electrónico'],
            'CAP → Confirmación Parcial de Comprobante Electrónico'  => ['CAP', 'Confirmación Parcial de Comprobante Electrónico'],
            'CR  → Confirmación de Rechazo de Comprobante'           => ['CR',  'Confirmación de Rechazo de Comprobante Electrónico'],
            'unknown → Comprobante Electrónico'                      => ['XXX', 'Comprobante Electrónico'],
        ];
    }
}
