<?php

namespace Tests\Unit\Services;

use App\DTOs\CatalogPageData;
use App\Enums\LandingPageVersion;
use App\Services\CatalogTemplateLocator;
use App\Services\CatalogTemplateRenderer;
use RuntimeException;
use Tests\TestCase;

class CatalogTemplateRendererTest extends TestCase
{
    public function test_it_wires_catalog_json_into_the_variation_template(): void
    {
        config(['landing-page-templates.path' => base_path('tests/Fixtures/catalog-templates')]);

        $rendered = (new CatalogTemplateRenderer(new CatalogTemplateLocator))->render(
            LandingPageVersion::Benefits,
            CatalogPageData::fromArray($this->catalogPayload()),
            'template-1',
        );

        $this->assertSame('Find the right watering can.', $rendered->title);
        $this->assertStringContainsString('<!doctype html>', strtolower($rendered->html));
        $this->assertStringContainsString('href="style.css"', $rendered->html);
        $this->assertStringContainsString('src="script.js"', $rendered->html);
        $this->assertStringContainsString('fonts.googleapis.com', $rendered->html);
        $this->assertStringContainsString('<body class="wired-theme">', $rendered->html);
        $this->assertStringContainsString('class="wired"', $rendered->html);
        $this->assertStringContainsString('Watering Can', $rendered->html);
        $this->assertStringContainsString('https://example.com/can', $rendered->html);
        $this->assertStringContainsString('https://cdn.example.com/can.png', $rendered->html);
        $this->assertSame('.wired { color: blue; }', trim($rendered->css));
        $this->assertSame("console.log('fixture');", trim($rendered->js));
        $this->assertStringContainsString('landing-page-templates/benefits/template-1/index.phtml', $rendered->prompt);
    }

    public function test_it_wires_a_second_template_in_the_same_variation(): void
    {
        config(['landing-page-templates.path' => base_path('tests/Fixtures/catalog-templates')]);

        $rendered = (new CatalogTemplateRenderer(new CatalogTemplateLocator))->render(
            LandingPageVersion::Benefits,
            CatalogPageData::fromArray($this->catalogPayload()),
            'template-2',
        );

        $this->assertStringContainsString('class="wired-alt"', $rendered->html);
        $this->assertSame('.wired-alt { color: green; }', trim($rendered->css));
    }

    public function test_it_can_include_multiple_production_templates_in_one_process(): void
    {
        config(['landing-page-templates.path' => resource_path('landing-page-templates')]);

        $renderer = new CatalogTemplateRenderer(new CatalogTemplateLocator);
        $catalog = CatalogPageData::fromArray($this->catalogPayload());

        foreach (LandingPageVersion::cases() as $version) {
            $rendered = $renderer->render($version, $catalog, 'template-2');

            $this->assertNotSame('', $rendered->html);
        }

        $comparisonTemplateOne = $renderer->render(LandingPageVersion::Comparison, $catalog, 'template-1');

        $this->assertNotSame('', $comparisonTemplateOne->html);
    }

    public function test_it_fails_when_the_variation_template_is_missing(): void
    {
        config(['landing-page-templates.path' => storage_path('framework/testing/missing-catalog-templates')]);

        $this->expectException(RuntimeException::class);
        $this->expectExceptionMessage('Add index.phtml to resources/landing-page-templates/offer/template-1');

        (new CatalogTemplateRenderer(new CatalogTemplateLocator))->render(
            LandingPageVersion::Offer,
            CatalogPageData::fromArray($this->catalogPayload()),
            'template-1',
        );
    }

    /**
     * @return array<string, mixed>
     */
    private function catalogPayload(): array
    {
        return [
            'Brand' => 'garden-tools-v1',
            'advertiser_id' => '15881',
            'campaign_id' => '122609',
            'subcategory' => 'Garden Tools',
            'category' => 'Home',
            'updated' => 'May 2026',
            'hero_title' => 'Find the right watering can.',
            'review_sources' => [],
            'products' => [
                [
                    'name' => 'Watering Can',
                    'brand' => 'Hoe',
                    'best_for' => 'Best Overall',
                    'price' => '$18',
                    'list_price' => '',
                    'discount' => '',
                    'rating' => '4.7',
                    'review_count' => '12',
                    'price_history' => '',
                    'image_url' => 'https://cdn.example.com/can.png',
                    'retailer_url' => 'https://example.com/can',
                    'chips' => ['Galvanized'],
                    'convertibility' => '',
                    'certification' => '',
                    'summary' => 'A steel watering can.',
                    'summary_short' => 'A steel watering can.',
                    'why_it_stands_out' => 'It will not rust.',
                    'why_short' => 'It will not rust.',
                    'pros' => ['Durable'],
                    'cons' => ['Heavy'],
                    'tradeoff_note' => 'Sturdy but heavy.',
                    'sources_note' => 'Shopper favorite.',
                ],
            ],
        ];
    }
}
