<?php

namespace Tests\Feature;

use App\Models\Asset;
use App\Models\Creative;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class CreativeShowProgressTest extends TestCase
{
    use RefreshDatabase;

    public function test_processing_creative_shows_render_percentage_and_stage(): void
    {
        $user = User::factory()->create();
        $creative = Creative::create([
            'user_id' => $user->id,
            'type' => 'video',
            'status' => 'processing',
            'source' => 'prompt',
            'prompt' => 'A video ad.',
            'ratio' => '9:16',
            'duration' => 30,
            'render_progress' => 52,
            'render_stage' => 'Extending scene 2 of 3',
        ]);

        $this->actingAs($user)
            ->get(route('creatives.show', $creative))
            ->assertOk()
            ->assertSee('52%')
            ->assertSee('Extending scene 2 of 3')
            ->assertSee('wire:poll.5s="refreshStatus"', escape: false)
            ->assertDontSee('<meta http-equiv="refresh"', escape: false)
            ->assertSee('aria-label="Estimated render progress"', escape: false)
            ->assertSee('aria-valuenow="52"', escape: false);

        // Once completed, polling stops and download button appears
        $creative->update([
            'status' => 'completed',
            'asset_path' => 'creatives/test.mp4',
            'render_progress' => 100,
        ]);

        $this->actingAs($user)
            ->get(route('creatives.show', $creative))
            ->assertOk()
            ->assertDontSee('wire:poll.5s="refreshStatus"', escape: false)
            ->assertDontSee('<meta http-equiv="refresh"', escape: false)
            ->assertSee('Download');
    }

    public function test_creative_show_renders_array_script_without_error(): void
    {
        $user = User::factory()->create();
        $creative = Creative::create([
            'user_id' => $user->id,
            'type' => 'video',
            'status' => 'completed',
            'source' => 'competitor',
            'ratio' => '9:16',
            'duration' => 15,
            'title' => 'Test Video Ad',
            'script' => [
                'hook' => 'Your partner heard that.',
                'problem' => 'Mouth-opening can amplify snoring.',
                'product' => 'AirFlow gently supports your jaw.',
                'benefits' => 'Enjoy quieter nights.',
                'cta' => 'Sleep More Comfortably—Shop Now.',
            ],
        ]);

        $this->actingAs($user)
            ->get(route('creatives.show', $creative))
            ->assertOk()
            ->assertSee('Your partner heard that.')
            ->assertSee('Hook:')
            ->assertSee('Mouth-opening can amplify snoring.')
            ->assertSee('Problem:')
            ->assertSee('AirFlow gently supports your jaw.')
            ->assertSee('Product:')
            ->assertSee('Sleep More Comfortably—Shop Now.')
            ->assertSee('Cta:');
    }

    public function test_creative_show_renders_indexed_list_script_without_error(): void
    {
        $user = User::factory()->create();
        $creative = Creative::create([
            'user_id' => $user->id,
            'type' => 'video',
            'status' => 'completed',
            'source' => 'prompt',
            'ratio' => '9:16',
            'duration' => 15,
            'title' => 'Test Video Ad 2',
            'script' => [
                'Scene 1: Introduction line',
                'Scene 2: Demonstration line',
            ],
        ]);

        $this->actingAs($user)
            ->get(route('creatives.show', $creative))
            ->assertOk()
            ->assertSee('Scene 1: Introduction line')
            ->assertSee('Scene 2: Demonstration line');
    }

    public function test_image_creative_renders_full_resolution_preview_url(): void
    {
        $user = User::factory()->create();
        $asset = Asset::create([
            'owner_type' => $user->getMorphClass(),
            'owner_id' => $user->id,
            'kind' => 'image',
            'source' => 'generated',
            'disk' => 'public',
            'path' => 'creatives/images/test.jpg',
            'original_name' => 'test.jpg',
            'mime' => 'image/jpeg',
            'bytes' => 1000,
        ]);
        $creative = Creative::create([
            'user_id' => $user->id,
            'type' => 'image',
            'status' => 'completed',
            'source' => 'prompt',
            'ratio' => '16:9',
            'asset_id' => $asset->id,
        ]);

        $this->actingAs($user)
            ->get(route('creatives.show', $creative))
            ->assertOk()
            ->assertSee(route('assets.preview', ['asset' => $asset, 'full' => 1]), escape: false);
    }

    public function test_creative_page_shows_the_brief_handling(): void
    {
        $user = User::factory()->create();
        $creative = Creative::create([
            'user_id' => $user->id,
            'type' => 'video',
            'status' => 'completed',
            'source' => 'product_url',
            'ratio' => '9:16',
            'duration' => 15,
            'brief' => ['product_name' => 'HP Smart Tank printer', 'handling' => 'display'],
        ]);

        $this->actingAs($user)
            ->get(route('creatives.show', $creative))
            ->assertOk()
            ->assertSee('Handling')
            ->assertSee('Display — product stays in place');
    }
}
