<?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_versions', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('artwork_id')->index();

            $table->unsignedInteger('version_number');
            $table->string('file_path');
            $table->string('file_hash', 64);
            $table->unsignedInteger('width')->nullable();
            $table->unsignedInteger('height')->nullable();
            $table->unsignedBigInteger('file_size')->nullable();
            $table->text('change_note')->nullable();
            $table->boolean('is_current')->default(false);
            $table->timestamps();

            $table->foreign('artwork_id', 'fk_artwork_versions_artwork')
                ->references('id')->on('artworks')->cascadeOnDelete();

            $table->unique(['artwork_id', 'version_number'], 'uq_artwork_version');
            $table->index(['artwork_id', 'is_current'], 'idx_artwork_version_current');
        });
    }

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