<?php

declare(strict_types=1);

use App\Jobs\GenerateArtworkEmbeddingJob;
use App\Models\Artwork;
use App\Models\ArtworkEmbedding;
use App\Models\Category;
use App\Models\ContentType;
use App\Models\Tag;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;

uses(RefreshDatabase::class);

it('persists a normalized embedding and upserts the artwork to the vector gateway', function () {
    config()->set('recommendations.embedding.enabled', true);
    config()->set('recommendations.embedding.endpoint', '/embed');
    config()->set('recommendations.embedding.min_dim', 2);
    config()->set('vision.clip.base_url', 'https://clip.local');
    config()->set('vision.vector_gateway.enabled', true);
    config()->set('vision.vector_gateway.base_url', 'https://vision.local');
    config()->set('vision.vector_gateway.api_key', 'test-key');
    config()->set('vision.vector_gateway.upsert_endpoint', '/vectors/upsert');
    config()->set('cdn.files_url', 'https://files.local');

    Http::fake([
        'https://clip.local/embed' => Http::response([
            'embedding' => [3.0, 4.0],
        ], 200),
        'https://vision.local/vectors/upsert' => Http::response([
            'status' => 'ok',
        ], 200),
    ]);

    $contentType = ContentType::query()->create([
        'name' => 'Wallpapers',
        'slug' => 'wallpapers',
        'description' => '',
    ]);

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

    $artwork = Artwork::factory()->create([
        'hash' => 'aabbccddeeff1122',
        'thumb_ext' => 'webp',
        'is_public' => true,
        'is_approved' => true,
        'published_at' => now()->subDay(),
    ]);
    $artwork->categories()->attach($category->id);
    $tag = Tag::query()->create(['name' => 'Neon', 'slug' => 'neon']);
    $artwork->tags()->attach($tag->id, ['source' => 'ai', 'confidence' => 0.91]);

    $job = new GenerateArtworkEmbeddingJob($artwork->id, 'aabbccddeeff1122');
    $job->handle(
        app(\App\Services\Vision\ArtworkEmbeddingClient::class),
        app(\App\Services\Vision\ArtworkVisionImageUrl::class),
        app(\App\Services\Vision\ArtworkVectorIndexService::class),
    );

    $embedding = ArtworkEmbedding::query()->where('artwork_id', $artwork->id)->first();
    $artwork->refresh();

    expect($embedding)->not->toBeNull()
        ->and($embedding?->dim)->toBe(2)
        ->and($embedding?->is_normalized)->toBeTrue()
        ->and($artwork->last_vector_indexed_at)->not->toBeNull();

    $vector = json_decode((string) $embedding?->embedding_json, true, 512, JSON_THROW_ON_ERROR);

    expect(round((float) $vector[0], 4))->toBe(0.6)
        ->and(round((float) $vector[1], 4))->toBe(0.8);

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

        $data = $request->data();

        return ($data['id'] ?? null) === (string) $artwork->id
            && ($data['url'] ?? null) === 'https://files.local/artworks/md/aa/bb/aabbccddeeff1122.webp'
            && ($data['metadata']['content_type'] ?? null) === 'Wallpapers'
            && ($data['metadata']['category'] ?? null) === 'Abstract'
            && ($data['metadata']['tags'] ?? null) === ['neon']
            && ($data['metadata']['user_id'] ?? null) === (string) $artwork->user_id
            && ($data['metadata']['is_public'] ?? null) === true
            && ($data['metadata']['is_deleted'] ?? null) === false
            && ($data['metadata']['is_nsfw'] ?? null) === false
            && isset($data['metadata']['category_id'])
            && isset($data['metadata']['content_type_id'])
            && array_key_exists('status', $data['metadata']);
    });
});

it('keeps the local embedding when vector upsert fails', function () {
    config()->set('recommendations.embedding.enabled', true);
    config()->set('recommendations.embedding.endpoint', '/embed');
    config()->set('recommendations.embedding.min_dim', 2);
    config()->set('vision.clip.base_url', 'https://clip.local');
    config()->set('vision.vector_gateway.enabled', true);
    config()->set('vision.vector_gateway.base_url', 'https://vision.local');
    config()->set('vision.vector_gateway.api_key', 'test-key');
    config()->set('vision.vector_gateway.upsert_endpoint', '/vectors/upsert');
    config()->set('cdn.files_url', 'https://files.local');

    Http::fake([
        'https://clip.local/embed' => Http::response([
            'embedding' => [1.0, 2.0, 2.0],
        ], 200),
        'https://vision.local/vectors/upsert' => Http::response([
            'message' => 'gateway error',
        ], 500),
    ]);

    $artwork = Artwork::factory()->create([
        'hash' => '1122334455667788',
        'thumb_ext' => 'webp',
        'is_public' => true,
        'is_approved' => true,
        'published_at' => now()->subDay(),
    ]);

    $job = new GenerateArtworkEmbeddingJob($artwork->id, '1122334455667788');
    $job->handle(
        app(\App\Services\Vision\ArtworkEmbeddingClient::class),
        app(\App\Services\Vision\ArtworkVisionImageUrl::class),
        app(\App\Services\Vision\ArtworkVectorIndexService::class),
    );

    $artwork->refresh();

    expect(ArtworkEmbedding::query()->where('artwork_id', $artwork->id)->exists())->toBeTrue()
        ->and($artwork->last_vector_indexed_at)->toBeNull();

    Http::assertSentCount(2);
});
