<?php

namespace Tests\Unit\Services\AI;

use App\DTOs\PromptLandingPageBrief;
use App\Services\AI\LaravelAiPromptLandingPageHtmlGenerator;
use Laravel\Ai\StructuredAnonymousAgent;
use Tests\TestCase;

class LaravelAiPromptLandingPageHtmlGeneratorTest extends TestCase
{
    public function test_prompt_uses_accent_color_as_the_theme(): void
    {
        StructuredAnonymousAgent::fake([
            [
                'title' => 'Austin Yoga',
                'html' => '<section><h1>Yoga with us</h1></section>',
                'css' => 'body { margin: 0; }',
                'js' => '',
            ],
        ]);

        $generated = (new LaravelAiPromptLandingPageHtmlGenerator)->generate(
            PromptLandingPageBrief::fromArray([
                'brand_name' => 'Austin Yoga',
                'offering' => 'Studio membership',
                'headline' => 'Find your calm in Austin',
                'features' => ['Small classes'],
                'benefits' => ['Less stress'],
                'pricing' => '$99 / month',
                'audience' => 'Busy professionals',
                'usps' => ['Downtown location'],
                'pain_points' => ['Crowded gyms'],
                'tone' => 'Calm and warm',
                'cta' => 'Book a class',
                'primary_color' => '#0f766e',
                'secondary_color' => '#14b8a6',
                'accent_color' => '#0d9488',
                'background_color' => '#f7fffc',
                'text_color' => '#0f172a',
                'purpose' => 'Promote studio memberships',
                'notes' => 'Earth tones',
            ]),
        );

        $this->assertStringContainsString('Purpose: Promote studio memberships', $generated->prompt);
        $this->assertStringContainsString('Theme / accent color: #0d9488', $generated->prompt);
        $this->assertStringContainsString('The Theme / accent color is the theme', $generated->prompt);
        $this->assertStringContainsString('the only CTA must be "Return to Austin Yoga"', $generated->prompt);
        $this->assertStringContainsString('as a link to "/" — the website index', $generated->prompt);
        $this->assertStringContainsString('Do not add "Continue to secure signup"', $generated->prompt);
        $this->assertStringContainsString('always add proper HTML validation even when the brief does not mention it', $generated->prompt);
        $this->assertStringContainsString('Do not set html or body to height: 100% with overflow: hidden', $generated->prompt);
        $this->assertStringContainsString('Buttons, links, header highlights, and CTAs must use var(--theme)', $generated->prompt);
        $this->assertStringContainsString('as a solid fill', $generated->prompt);
        $this->assertStringContainsString('Do not use primary as the main button or header color', $generated->prompt);
        $this->assertStringContainsString('#0f766e', $generated->prompt);
        $this->assertStringContainsString('#f7fffc', $generated->prompt);
        $this->assertStringContainsString('--theme: #0d9488', $generated->css);
        $this->assertStringContainsString('--accent: #0d9488', $generated->css);
        $this->assertStringContainsString('background-color: var(--theme)', $generated->css);
        $this->assertStringContainsString('Logo present: no', $generated->prompt);
        $this->assertStringContainsString('use the brand name as text in the header', $generated->prompt);
    }

    public function test_prompt_includes_logo_redraw_rules_when_a_logo_is_present(): void
    {
        StructuredAnonymousAgent::fake([
            [
                'title' => 'Austin Yoga',
                'html' => '<section><h1>Yoga with us</h1></section>',
                'css' => 'body { margin: 0; }',
                'js' => '',
            ],
        ]);

        $generated = (new LaravelAiPromptLandingPageHtmlGenerator)->generate(
            PromptLandingPageBrief::fromArray([
                'brand_name' => 'Austin Yoga',
                'offering' => 'Studio membership',
                'headline' => 'Find your calm in Austin',
                'features' => ['Small classes'],
                'benefits' => ['Less stress'],
                'pricing' => '$99 / month',
                'audience' => 'Busy professionals',
                'usps' => ['Downtown location'],
                'pain_points' => ['Crowded gyms'],
                'tone' => 'Calm and warm',
                'cta' => 'Book a class',
                'primary_color' => '#0f766e',
                'secondary_color' => '#14b8a6',
                'accent_color' => '#0d9488',
                'background_color' => '#f7fffc',
                'text_color' => '#0f172a',
                'logo_present' => true,
                'logo_type' => 'combination-mark',
                'logo_wordmark_text' => 'Austin Yoga',
                'logo_description' => 'A teal lotus icon beside the wordmark Austin Yoga.',
                'notes' => 'Earth tones',
            ]),
        );

        $this->assertStringContainsString('Logo present: yes', $generated->prompt);
        $this->assertStringContainsString('Logo type: combination-mark', $generated->prompt);
        $this->assertStringContainsString('Logo wordmark text: Austin Yoga', $generated->prompt);
        $this->assertStringContainsString('A teal lotus icon beside the wordmark Austin Yoga.', $generated->prompt);
        $this->assertStringContainsString('draw a simple inline SVG header logo', $generated->prompt);
        $this->assertStringContainsString('Do not use an <img> of an uploaded file', $generated->prompt);
        $this->assertStringContainsString('Logo URL: none', $generated->prompt);
    }

    public function test_prompt_and_css_use_the_brief_font_family(): void
    {
        StructuredAnonymousAgent::fake([
            [
                'title' => 'Austin Yoga',
                'html' => '<section><h1>Yoga with us</h1></section>',
                'css' => 'body { margin: 0; }',
                'js' => '',
            ],
        ]);

        $generated = (new LaravelAiPromptLandingPageHtmlGenerator)->generate(
            PromptLandingPageBrief::fromArray([
                'brand_name' => 'Austin Yoga',
                'offering' => 'Studio membership',
                'headline' => 'Find your calm in Austin',
                'features' => ['Small classes'],
                'benefits' => ['Less stress'],
                'pricing' => '$99 / month',
                'audience' => 'Busy professionals',
                'usps' => ['Downtown location'],
                'pain_points' => ['Crowded gyms'],
                'tone' => 'Calm and warm',
                'cta' => 'Book a class',
                'primary_color' => '#0f766e',
                'secondary_color' => '#14b8a6',
                'accent_color' => '#0d9488',
                'background_color' => '#f7fffc',
                'text_color' => '#0f172a',
                'font_family' => 'Inter',
                'font_stylesheet_url' => 'https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap',
                'notes' => 'Earth tones',
            ]),
        );

        $this->assertStringContainsString('Font family: Inter', $generated->prompt);
        $this->assertStringContainsString(
            'Font stylesheet URL: https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap',
            $generated->prompt,
        );
        $this->assertStringContainsString('If Font family is not none, use that exact typeface', $generated->prompt);
        $this->assertStringContainsString('--font: Inter, sans-serif', $generated->css);
        $this->assertStringContainsString('font-family: var(--font)', $generated->css);
        $this->assertStringStartsWith(
            '@import url("https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap");',
            $generated->css,
        );
    }

    public function test_prompt_allows_a_public_header_logo_url(): void
    {
        StructuredAnonymousAgent::fake([
            [
                'title' => 'Lucos',
                'html' => '<section><h1>Lucos</h1></section>',
                'css' => 'body { margin: 0; }',
                'js' => '',
            ],
        ]);

        $generated = (new LaravelAiPromptLandingPageHtmlGenerator)->generate(
            PromptLandingPageBrief::fromArray([
                'brand_name' => 'Lucos',
                'offering' => 'Web services',
                'headline' => 'Build with Lucos',
                'features' => ['Support'],
                'benefits' => ['Faster sites'],
                'pricing' => 'Contact us',
                'audience' => 'Businesses',
                'usps' => ['Local team'],
                'pain_points' => ['Slow sites'],
                'tone' => 'Professional',
                'cta' => 'Get a quote',
                'primary_color' => '#7c5cff',
                'secondary_color' => '#1e1b4b',
                'accent_color' => '#7c5cff',
                'background_color' => '#ffffff',
                'text_color' => '#111111',
                'logo_present' => true,
                'logo_type' => 'combination-mark',
                'logo_wordmark_text' => 'Lucos',
                'logo_description' => 'White wordmark on a purple header.',
                'logo_url' => 'https://lucos.example.com/assets/img/logo-light.png',
                'favicon_url' => 'https://lucos.example.com/favicon.ico',
                'notes' => 'Use the live logo.',
            ]),
        );

        $this->assertStringContainsString('Logo URL: https://lucos.example.com/assets/img/logo-light.png', $generated->prompt);
        $this->assertStringContainsString('Favicon URL: https://lucos.example.com/favicon.ico', $generated->prompt);
        $this->assertStringContainsString('use that URL as the header <img> src', $generated->prompt);
        $this->assertStringContainsString('live website\'s public logo or favicon', $generated->prompt);
        $this->assertStringContainsString('Do not add a <link rel="icon"> in the html snippet', $generated->prompt);
    }

    public function test_app_download_prompt_uses_download_cta_rules_instead_of_a_required_lead_form(): void
    {
        StructuredAnonymousAgent::fake([
            [
                'title' => 'Yoga App',
                'html' => '<section><a href="https://apps.apple.com/app/yoga">Download</a></section>',
                'css' => 'body { margin: 0; }',
                'js' => '',
            ],
        ]);

        $generated = (new LaravelAiPromptLandingPageHtmlGenerator)->generate(
            PromptLandingPageBrief::fromArray([
                'brand_name' => 'Austin Yoga',
                'offering' => 'Mobile app',
                'headline' => 'Practice anywhere',
                'features' => ['Guided classes'],
                'benefits' => ['Train on the go'],
                'pricing' => 'Free',
                'audience' => 'Busy professionals',
                'usps' => ['Offline mode'],
                'pain_points' => ['No studio nearby'],
                'tone' => 'Energetic',
                'cta' => 'Download the app',
                'primary_color' => '#0f766e',
                'secondary_color' => '#14b8a6',
                'accent_color' => '#0d9488',
                'background_color' => '#f7fffc',
                'text_color' => '#0f172a',
                'purpose' => 'App download page',
                'notes' => 'Use https://apps.apple.com/app/yoga as the download URL.',
            ]),
        );

        $this->assertStringContainsString('Purpose: App download page', $generated->prompt);
        $this->assertStringContainsString('This page purpose is App download', $generated->prompt);
        $this->assertStringContainsString('Do not require a lead form', $generated->prompt);
        $this->assertStringContainsString('data-admedia-download="1"', $generated->prompt);
        $this->assertStringContainsString('Do not invent an App Store, Play Store, or APK URL', $generated->prompt);
        $this->assertStringContainsString('Do not add a "Return to" website link', $generated->prompt);
        $this->assertStringNotContainsString('the only CTA must be "Return to Austin Yoga"', $generated->prompt);
        $this->assertStringContainsString('Buttons, links, header highlights, and CTAs must use var(--theme)', $generated->prompt);
    }
}
