<?php

declare(strict_types=1);

use App\Models\Tag;

it('renders the tag page with correct title and canonical', function (): void {
    $tag = Tag::factory()->create(['name' => 'Cyberpunk', 'slug' => 'cyberpunk', 'is_active' => true]);

    $response = $this->get('/tag/cyberpunk');

    $response->assertOk();
    $html = $response->getContent();
    expect($html)
        ->toContain('Cyberpunk')
        ->toContain('index,follow');
});

it('returns 404 for a non-existent tag slug', function (): void {
    $this->get('/tag/does-not-exist-xyz')->assertNotFound();
});

it('renders tag page with artworks from the tag', function (): void {
    // The tag page uses Meilisearch (SCOUT_DRIVER=null in tests → empty results).
    // We verify the page renders correctly with tag metadata; artwork grid
    // content is covered by browser/e2e tests against a live index.
    $tag = Tag::factory()->create(['name' => 'Night', 'slug' => 'night', 'is_active' => true]);

    $response = $this->get('/tag/night');

    $response->assertOk();
    expect($response->getContent())
        ->toContain('Night')
        ->toContain('index,follow');
});

it('shows pagination rel links on tag pages with enough artworks', function (): void {
    // NOTE: pagination rel links are injected only when the Meilisearch paginator
    // returns > per_page results. SCOUT_DRIVER=null returns an empty paginator
    // in feature tests, so we only assert the page renders without error.
    // Full pagination behaviour is verified via e2e tests.
    Tag::factory()->create(['name' => 'Nature', 'slug' => 'nature', 'is_active' => true]);

    $this->get('/tag/nature')->assertOk();
});

it('includes JSON-LD CollectionPage schema on tag pages', function (): void {
    Tag::factory()->create(['name' => 'Abstract', 'slug' => 'abstract', 'is_active' => true]);

    $html = $this->get('/tag/abstract')->assertOk()->getContent();

    expect($html)
        ->toContain('application/ld+json')
        ->toContain('CollectionPage');
});

it('supports sort parameter without error', function (): void {
    Tag::factory()->create(['name' => 'Space', 'slug' => 'space', 'is_active' => true]);

    foreach (['popular', 'latest', 'likes', 'downloads'] as $sort) {
        $this->get("/tag/space?sort={$sort}")->assertOk();
    }
});
