<?php

namespace Tests\Feature;

use App\Enums\LandingPageVersion;
use App\Models\LandingPage;
use App\Models\LandingPageGeneration;
use App\Models\PromptLandingPage;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class LandingPagesControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_guests_are_redirected_from_the_landing_pages_hub(): void
    {
        $this->get(route('landing-pages.index'))
            ->assertRedirect(route('login'));
    }

    public function test_it_shows_the_catalog_tab_by_default(): void
    {
        $user = User::factory()->create();
        LandingPageGeneration::factory()
            ->catalog()
            ->create([
                'user_id' => $user->id,
                'brand_name' => 'Office Depot',
            ]);

        $this->actingAs($user)
            ->get(route('landing-pages.index'))
            ->assertOk()
            ->assertSee('Landing pages', false)
            ->assertSee('Catalog', false)
            ->assertSee('Prompt', false)
            ->assertSee('Office Depot', false)
            ->assertSee('New catalog page', false)
            ->assertSee('<input type="hidden" name="tab" value="catalog">', false);
    }

    public function test_it_shows_the_prompt_tab_when_requested(): void
    {
        $user = User::factory()->create();
        PromptLandingPage::factory()->create([
            'user_id' => $user->id,
            'title' => 'Summer promo page',
        ]);

        $this->actingAs($user)
            ->get(route('landing-pages.index', ['tab' => 'prompt']))
            ->assertOk()
            ->assertSee('Summer promo page', false)
            ->assertSee('New prompt page', false)
            ->assertSee('<input type="hidden" name="tab" value="prompt">', false);
    }

    public function test_prompt_purpose_filter_stays_on_the_prompt_tab(): void
    {
        $user = User::factory()->create();
        PromptLandingPage::factory()->for($user)->create([
            'title' => 'Yoga app download',
            'purpose' => 'App download page',
        ]);
        LandingPageGeneration::factory()
            ->catalog()
            ->create([
                'user_id' => $user->id,
                'brand_name' => 'Office Depot Catalog Brand',
            ]);

        $this->actingAs($user)
            ->get(route('landing-pages.index', [
                'tab' => 'prompt',
                'purpose' => 'App download page',
            ]))
            ->assertOk()
            ->assertSee('Yoga app download', false)
            ->assertSee('New prompt page', false)
            ->assertSee('<input type="hidden" name="tab" value="prompt">', false)
            ->assertDontSee('Office Depot Catalog Brand', false);
    }

    public function test_the_prompt_tab_links_to_the_editor_when_the_page_is_pending_approval(): void
    {
        $user = User::factory()->create();
        $page = PromptLandingPage::factory()->pendingApproval()->for($user)->create([
            'title' => 'Pending leads page',
            'purpose' => 'Leads Page',
        ]);

        $this->actingAs($user)
            ->get(route('landing-pages.index', ['tab' => 'prompt']))
            ->assertOk()
            ->assertSee('aria-label="Edit page"', false)
            ->assertSee(route('prompt-landing-pages.editor', $page), false);
    }

    public function test_the_prompt_tab_hides_the_editor_when_the_page_is_approved(): void
    {
        $user = User::factory()->create();
        $page = PromptLandingPage::factory()->approved()->for($user)->create([
            'title' => 'Approved leads page',
            'purpose' => 'Leads Page',
        ]);

        $this->actingAs($user)
            ->get(route('landing-pages.index', ['tab' => 'prompt']))
            ->assertOk()
            ->assertDontSee('aria-label="Edit page"', false)
            ->assertDontSee(route('prompt-landing-pages.editor', $page), false);
    }

    public function test_legacy_catalog_index_redirects_to_the_catalog_tab(): void
    {
        $user = User::factory()->create();

        $this->actingAs($user)
            ->get(route('catalog-landing-pages.index', ['q' => 'office']))
            ->assertRedirect(route('landing-pages.index', ['tab' => 'catalog', 'q' => 'office']));
    }

    public function test_legacy_prompt_index_redirects_to_the_prompt_tab(): void
    {
        $user = User::factory()->create();

        $this->actingAs($user)
            ->get(route('prompt-landing-pages.index', ['status' => 'approved']))
            ->assertRedirect(route('landing-pages.index', ['tab' => 'prompt', 'status' => 'approved']));
    }

    public function test_catalog_live_urls_only_appear_after_affiliate_tracking_is_saved(): void
    {
        $user = User::factory()->create();
        $generation = LandingPageGeneration::factory()
            ->catalog()
            ->withPartnerWebsite('https://rateit.com')
            ->create([
                'user_id' => $user->id,
                'status' => 'completed',
            ]);

        LandingPage::factory()
            ->for($generation, 'generation')
            ->completed()
            ->create([
                'version' => LandingPageVersion::Benefits,
            ]);

        $this->actingAs($user)
            ->get(route('landing-pages.index', ['tab' => 'catalog']))
            ->assertOk()
            ->assertDontSee('Live URL', false);
    }
}
