<?php

declare(strict_types=1);

use App\Models\Artwork;
use App\Models\Category;
use App\Models\ContentType;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use function Pest\Laravel\artisan;

uses(RefreshDatabase::class);

beforeEach(function (): void {
    config()->set('vision.vector_gateway.enabled', true);
    config()->set('vision.vector_gateway.base_url', 'https://vision.klevze.net');
    config()->set('vision.vector_gateway.api_key', 'test-key');
    config()->set('vision.vector_gateway.upsert_endpoint', '/vectors/upsert');
    config()->set('vision.vector_gateway.search_endpoint', '/vectors/search');
    config()->set('vision.image_variant', 'md');
    config()->set('cdn.files_url', 'https://files.skinbase.org');
});

it('indexes artworks into the vector gateway with artwork metadata', function (): void {
    $contentType = ContentType::query()->create([
        'name' => 'Photography',
        'slug' => 'photography',
        'description' => '',
    ]);

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

    $artwork = Artwork::factory()->create([
        'hash' => 'aabbcc112233',
        'thumb_ext' => 'webp',
    ]);
    $artwork->categories()->attach($category->id);

    Http::fake([
        'https://vision.klevze.net/vectors/upsert' => Http::response(['ok' => true], 200),
    ]);

    artisan('artworks:vectors-index', ['--limit' => 1])
        ->assertSuccessful();

    Http::assertSent(function ($request) use ($artwork): bool {
        if ($request->url() !== 'https://vision.klevze.net/vectors/upsert') {
            return false;
        }

        $payload = json_decode($request->body(), true);

        return $request->hasHeader('X-API-Key', 'test-key')
            && is_array($payload)
            && ($payload['id'] ?? null) === (string) $artwork->id
            && ($payload['url'] ?? null) === 'https://files.skinbase.org/md/aa/bb/aabbcc112233.webp'
            && ($payload['metadata']['content_type'] ?? null) === 'Photography'
            && ($payload['metadata']['category'] ?? null) === 'Abstract';
    });
});

it('searches similar artworks through the vector gateway', function (): void {
    $contentType = ContentType::query()->create([
        'name' => 'Wallpapers',
        'slug' => 'wallpapers',
        'description' => '',
    ]);

    $category = Category::query()->create([
        'content_type_id' => $contentType->id,
        'parent_id' => null,
        'name' => 'Nature',
        'slug' => 'nature',
        'description' => '',
        'is_active' => true,
        'sort_order' => 0,
    ]);

    $source = Artwork::factory()->create([
        'title' => 'Source artwork',
        'hash' => 'aabbcc112233',
        'thumb_ext' => 'webp',
    ]);
    $source->categories()->attach($category->id);

    $similar = Artwork::factory()->create([
        'title' => 'Nearby artwork',
        'hash' => 'ddeeff445566',
        'thumb_ext' => 'webp',
    ]);
    $similar->categories()->attach($category->id);

    Http::fake([
        'https://vision.klevze.net/vectors/search' => Http::response([
            'results' => [
                ['id' => $source->id, 'score' => 1.0],
                ['id' => $similar->id, 'score' => 0.9876],
            ],
        ], 200),
    ]);

    artisan('artworks:vectors-search', [
        'artwork_id' => $source->id,
        '--limit' => 5,
    ])
        ->expectsTable(['ID', 'Score', 'Title', 'Content Type', 'Category'], [[
            'id' => $similar->id,
            'score' => '0.9876',
            'title' => 'Nearby artwork',
            'content_type' => 'Wallpapers',
            'category' => 'Nature',
        ]])
        ->assertSuccessful();
});
