<?php

declare(strict_types=1);

require_once dirname(__DIR__, 2) . '/feca_mailshots_plugin/src/autoload.php';

use FecaMailshots\Application\TemplateRenderer;

$pngBytes = base64_decode(
    'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQAAAAA3bvkkAAAAIGNIUk0AAHomAACAhAAA+gAAAIDoAAB1MAAA6mAAADqYAAAXcJy6UTwAAAACYktHRAAB3YoTpAAAAAd0SU1FB+oEGAYzAgXOM1oAAAAKSURBVAjXY2gAAACCAIHdQ2r0AAAAJXRFWHRkYXRlOmNyZWF0ZQAyMDI2LTA0LTI0VDA2OjUxOjAyKzAwOjAwPufkWAAAACV0RVh0ZGF0ZTptb2RpZnkAMjAyNi0wNC0yNFQwNjo1MTowMiswMDowME+6XOQAAAAodEVYdGRhdGU6dGltZXN0YW1wADIwMjYtMDQtMjRUMDY6NTE6MDIrMDA6MDAYr307AAAAAElFTkSuQmCC',
    true
);
if (!is_string($pngBytes) || $pngBytes === '') {
    fwrite(STDERR, "Failed to decode png fixture bytes\n");
    exit(1);
}

$renderer = new TemplateRenderer(
    static function (string $name) use ($pngBytes): ?array {
        $assetName = strtolower(trim($name));
        if (!in_array($assetName, ['logo_asset', 'right_logo_asset'], true)) {
            return null;
        }
        return [
            'name' => $assetName,
            'file_name' => 'logo.png',
            'mime_type' => 'image/png',
            'file_bytes' => $pngBytes,
            'width_mm' => 20,
            'height_mm' => 10,
            'justification' => $assetName === 'right_logo_asset' ? 'right' : 'left',
        ];
    }
);

$rendered = $renderer->render(
    'Subject',
    '<p>Body</p>',
    '<div>{{ pdf_asset("logo_asset") }}</div>',
    []
);

$pdfHtml = (string) ($rendered['pdf_attachment'] ?? '');
if (strpos($pdfHtml, 'data:image/png;base64,') === false && strpos($pdfHtml, 'data:image/jpeg;base64,') === false) {
    fwrite(STDERR, "pdf_asset did not render png/jpeg data-uri image HTML\n");
    exit(1);
}
if (strpos($pdfHtml, 'text-align:left') === false) {
    fwrite(STDERR, "pdf_asset did not apply left justification wrapper\n");
    exit(1);
}
if (strpos($pdfHtml, 'width:20mm;height:10mm;') === false) {
    fwrite(STDERR, "pdf_asset did not apply width/height mm styles\n");
    exit(1);
}

$rightRendered = $renderer->render(
    'Subject',
    '<p>Body</p>',
    '<p>{{ pdf_asset("right_logo_asset") }}Text should wrap to the left of the floated image.</p>',
    []
);
$rightPdfHtml = (string) ($rightRendered['pdf_attachment'] ?? '');
if (strpos($rightPdfHtml, 'float:right') === false) {
    fwrite(STDERR, "pdf_asset did not float right-justified image\n");
    exit(1);
}
if (strpos($rightPdfHtml, 'text-align:right') !== false) {
    fwrite(STDERR, "pdf_asset right justification should not use a block text-align wrapper\n");
    exit(1);
}

$booleanRendered = $renderer->render(
    '{{ advertisers_islapsed }} {{ advertisers_selected }} {{ advertisers_advertisername }}',
    '<p>{{ advertisers_islapsed }} {{ advertisers_selected }} {{ advertisers_advertisername }}</p>',
    '',
    [
        'advertisers.IsLapsed?' => "\xff",
        'advertisers.Selected' => '0',
        'advertisers.AdvertiserName' => 'A R Aspinall & Sons Ltd',
    ]
);
if (($booleanRendered['subject'] ?? '') !== 'Yes No A R Aspinall & Sons Ltd') {
    fwrite(STDERR, "Boolean-ish template fields were not rendered readably: " . ($booleanRendered['subject'] ?? '') . "\n");
    exit(1);
}

try {
    $renderer->render('S', 'M', '{{ pdf_asset("missing_asset") }}', []);
    fwrite(STDERR, "Expected hard error for missing pdf_asset, but render succeeded\n");
    exit(1);
} catch (Throwable $e) {
    if (strpos($e->getMessage(), 'not found') === false) {
        fwrite(STDERR, "Unexpected missing-asset error message: {$e->getMessage()}\n");
        exit(1);
    }
}

echo "TemplateRenderer pdf_asset regression test passed\n";
