<?php

use App\Models\Artwork;
use App\Models\Category;
use App\Models\ContentType;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;

uses(RefreshDatabase::class);

function createContentTypeAndCategoryForSimilarTests(): array
{
    $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-similar',
        'description' => 'Abstract works',
        'is_active' => true,
        'sort_order' => 0,
    ]);

    return [$contentType, $category];
}

it('renders similar artworks block when similarities exist with algo version and CDN thumbnails', function () {
    [$contentType, $category] = createContentTypeAndCategoryForSimilarTests();

    $author = User::factory()->create();

    $source = Artwork::factory()->create([
        'user_id' => $author->id,
        'title' => 'Source Artwork',
        'slug' => 'source-artwork',
        'hash' => 'aa11bb22cc33',
        'thumb_ext' => 'webp',
        'published_at' => now()->subMinute(),
        'is_public' => true,
        'is_approved' => true,
    ]);

    $similar = Artwork::factory()->create([
        'user_id' => $author->id,
        'title' => 'Similar Artwork',
        'slug' => 'similar-artwork',
        'hash' => 'bb22cc33dd44',
        'thumb_ext' => 'webp',
        'published_at' => now()->subMinute(),
        'is_public' => true,
        'is_approved' => true,
    ]);

    $source->categories()->attach($category->id);
    $similar->categories()->attach($category->id);

    DB::table('artwork_similarities')->insert([
        'artwork_id' => $source->id,
        'similar_artwork_id' => $similar->id,
        'model' => 'clip',
        'model_version' => 'v1',
        'algo_version' => 'clip-cosine-v1',
        'rank' => 1,
        'score' => 0.9234567,
        'generated_at' => now(),
        'created_at' => now(),
        'updated_at' => now(),
    ]);

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

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

    $response->assertOk();
    $response->assertSee('Similar artworks');
    $response->assertSee('data-algo-version="clip-cosine-v1"', false);
    $response->assertSee((string) $similar->thumb_url, false);
    $response->assertSee('https://files.skinbase.org', false);
});

it('hides similar artworks block when no similarities exist', function () {
    [$contentType, $category] = createContentTypeAndCategoryForSimilarTests();

    $author = User::factory()->create();

    $source = Artwork::factory()->create([
        'user_id' => $author->id,
        'title' => 'Lonely Artwork',
        'slug' => 'lonely-artwork',
        'hash' => 'cc33dd44ee55',
        'thumb_ext' => 'webp',
        'published_at' => now()->subMinute(),
        'is_public' => true,
        'is_approved' => true,
    ]);

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

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

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

    $response->assertOk();
    $response->assertDontSee('Similar artworks', false);
    $response->assertDontSee('data-similar-analytics', false);
});
