<?php

declare(strict_types=1);

use App\Models\World;
use App\Models\WorldWebStory;
use App\Models\WorldWebStoryPage;
use App\Services\Sitemaps\SitemapBuildService;

it('renders a published world web story as an amp story document', function (): void {
    $world = World::factory()->current()->create([
        'title' => 'Fantasy Realms',
        'slug' => 'fantasy-realms-world',
    ]);

    $story = WorldWebStory::factory()->visible()->for($world)->create([
        'slug' => 'fantasy-realms',
        'title' => 'Fantasy Realms',
        'excerpt' => 'A cinematic journey through magical wallpapers, enchanted landscapes, and dreamlike digital art.',
        'poster_portrait_path' => 'https://files.skinbase.org/web-stories/worlds/fantasy-realms/poster-portrait.webp',
        'publisher_logo_path' => 'https://cdn.skinbase.org/images/skinbase_logo_96.webp',
    ]);

    foreach (range(1, 5) as $position) {
        WorldWebStoryPage::factory()->for($story, 'story')->create([
            'position' => $position,
            'headline' => 'Page ' . $position,
            'background_path' => 'https://files.skinbase.org/web-stories/worlds/fantasy-realms/pages/page-0' . $position . '.webp',
            'background_mobile_path' => 'https://files.skinbase.org/web-stories/worlds/fantasy-realms/pages/page-0' . $position . '.webp',
            'alt_text' => 'Page ' . $position,
        ]);
    }

    $response = $this->get(route('web-stories.show', ['slug' => $story->slug]));

    $response->assertOk();
    $response->assertSee('<html amp', false);
    $response->assertSee('<amp-story', false);
    $response->assertSee('publisher-logo-src="https://cdn.skinbase.org/images/skinbase_logo_96.webp"', false);
    $response->assertSee('poster-portrait-src="https://files.skinbase.org/web-stories/worlds/fantasy-realms/poster-portrait.webp"', false);
    $response->assertSee('<link rel="canonical" href="' . route('web-stories.show', ['slug' => $story->slug]) . '">', false);
    $response->assertDontSee('noindex,follow', false);
    expect(substr_count((string) $response->getContent(), '<amp-story-page '))->toBeGreaterThanOrEqual(5);
});

it('does not expose draft world web stories publicly', function (): void {
    $world = World::factory()->current()->create();
    $story = WorldWebStory::factory()->for($world)->create([
        'slug' => 'draft-world-story',
        'status' => WorldWebStory::STATUS_DRAFT,
    ]);

    $this->get(route('web-stories.show', ['slug' => $story->slug]))->assertNotFound();
});

it('renders the web stories index with published stories only', function (): void {
    $world = World::factory()->current()->create([
        'title' => 'Spring Vibes',
        'slug' => 'spring-vibes-world',
    ]);

    $visible = WorldWebStory::factory()->visible()->for($world)->create([
        'slug' => 'spring-vibes',
        'title' => 'Spring Vibes',
    ]);

    $draft = WorldWebStory::factory()->for($world)->create([
        'slug' => 'hidden-story',
        'title' => 'Hidden Story',
    ]);

    $response = $this->get(route('web-stories.index'));

    $response->assertOk();
    $response->assertSee($visible->title);
    $response->assertDontSee($draft->title);
});

it('includes visible web stories in the dedicated sitemap only', function (): void {
    $world = World::factory()->current()->create();

    $visible = WorldWebStory::factory()->visible()->for($world)->create([
        'slug' => 'hello-again',
    ]);

    WorldWebStory::factory()->published()->for($world)->create([
        'slug' => 'noindex-story',
        'noindex' => true,
    ]);

    WorldWebStory::factory()->for($world)->create([
        'slug' => 'draft-story',
    ]);

    $built = app(SitemapBuildService::class)->buildNamed('web-stories', force: true, persist: false);

    expect($built)->not->toBeNull();

    $path = public_path('sitemaps/web-stories.xml');
    if (! is_dir(dirname($path))) {
        mkdir(dirname($path), 0777, true);
    }
    file_put_contents($path, $built['content']);

    $this->get('/sitemaps/web-stories.xml')->assertOk();

    $xml = file_get_contents($path);

    expect(str_contains($xml, route('web-stories.show', ['slug' => $visible->slug])))->toBeTrue();
    expect(str_contains($xml, 'noindex-story'))->toBeFalse();
    expect(str_contains($xml, 'draft-story'))->toBeFalse();
});