<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('artwork_embeddings', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('artwork_id')->constrained('artworks')->cascadeOnDelete();
            $table->string('model', 64);
            $table->string('model_version', 64);
            $table->string('algo_version', 64)->default('clip-cosine-v1')->index();
            $table->unsignedSmallInteger('dim');
            $table->longText('embedding_json');
            $table->string('source_hash', 128)->nullable()->index();
            $table->boolean('is_normalized')->default(true);
            $table->timestamp('generated_at')->nullable()->index();
            $table->json('meta')->nullable();
            $table->timestamps();

            $table->unique(['artwork_id', 'model', 'model_version'], 'artwork_embeddings_artwork_model_unique');
            $table->index(['model', 'model_version'], 'artwork_embeddings_model_lookup_idx');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('artwork_embeddings');
    }
};
