<?php

use App\Models\Artwork;
use App\Models\Category;
use App\Models\ContentType;
use App\Models\Tag;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

uses(RefreshDatabase::class);

it('renders JSON-LD structured data on published artwork page', function () {
    $user = User::factory()->create(['name' => 'Schema Author']);

    $contentType = ContentType::create([
        'name' => 'Photography',
        'slug' => 'photography',
        'description' => 'Photography content',
    ]);

    $category = Category::create([
        'content_type_id' => $contentType->id,
        'parent_id' => null,
        'name' => 'Abstract',
        'slug' => 'abstract-' . Str::lower(Str::random(5)),
        'description' => 'Abstract works',
        'is_active' => true,
        'sort_order' => 0,
    ]);

    $artwork = Artwork::factory()->create([
        'user_id' => $user->id,
        'title' => 'Schema Ready Artwork',
        'slug' => 'schema-ready-artwork',
        'description' => 'Artwork description for schema test.',
        'mime_type' => 'image/jpeg',
        'published_at' => now()->subMinute(),
        'is_public' => true,
        'is_approved' => true,
    ]);

    $artwork->categories()->attach($category->id);

    $tagA = Tag::create(['name' => 'neon', 'slug' => 'neon', 'usage_count' => 0, 'is_active' => true]);
    $tagB = Tag::create(['name' => 'city', 'slug' => 'city', 'usage_count' => 0, 'is_active' => true]);
    $artwork->tags()->attach([
        $tagA->id => ['source' => 'user', 'confidence' => 0.9],
        $tagB->id => ['source' => 'user', 'confidence' => 0.8],
    ]);

    $html = view('artworks.show', ['artwork' => $artwork])->render();

    expect($html)
        ->toContain('application/ld+json')
        ->toContain('"@type":"ImageObject"')
        ->toContain('"name":"Schema Ready Artwork"')
        ->toContain('"keywords":["neon","city"]');
});

it('renders JSON-LD via routed artwork show endpoint', function () {
    $user = User::factory()->create(['name' => 'Schema Route Author']);

    $contentType = ContentType::create([
        'name' => 'Photography',
        'slug' => 'photography',
        'description' => 'Photography content',
    ]);

    $category = Category::create([
        'content_type_id' => $contentType->id,
        'parent_id' => null,
        'name' => 'Abstract',
        'slug' => 'abstract-route',
        'description' => 'Abstract works',
        'is_active' => true,
        'sort_order' => 0,
    ]);

    $artwork = Artwork::factory()->create([
        'user_id' => $user->id,
        'title' => 'Schema Route Artwork',
        'slug' => 'schema-route-artwork',
        'description' => 'Artwork description for routed schema test.',
        'mime_type' => 'image/png',
        'published_at' => now()->subMinute(),
        'is_public' => true,
        'is_approved' => true,
    ]);

    $artwork->categories()->attach($category->id);

    $tag = Tag::create(['name' => 'route-tag', 'slug' => 'route-tag', 'usage_count' => 0, 'is_active' => true]);
    $artwork->tags()->attach([
        $tag->id => ['source' => 'user', 'confidence' => 0.95],
    ]);

    $url = route('artworks.show', [
        'contentTypeSlug' => $contentType->slug,
        'categoryPath' => $category->slug,
        'artwork' => $artwork->slug,
    ]);

    expect($url)->toContain('/photography/abstract-route/schema-route-artwork');

    $matchedRoute = app('router')->getRoutes()->match(Request::create($url, 'GET'));
    expect($matchedRoute->getName())->toBe('artworks.show');

    $response = $this->get($url);

    $response->assertOk();
    $response->assertSee('application/ld+json', false);
    $response->assertSee('"@type":"ImageObject"', false);
    $response->assertSee('"name":"Schema Route Artwork"', false);
    $response->assertSee('"keywords":["route-tag"]', false);
});
