<?php

declare(strict_types=1);

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

return new class extends Migration {
    public function up(): void
    {
        if (Schema::hasTable('artwork_maturity_audit_findings')) {
            return;
        }

        Schema::create('artwork_maturity_audit_findings', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('artwork_id')->constrained('artworks')->cascadeOnDelete();
            $table->string('status', 24)->default('open')->index();
            $table->string('thumbnail_variant', 24)->nullable();
            $table->string('ai_label', 24)->nullable()->index();
            $table->decimal('ai_confidence', 5, 4)->nullable()->index();
            $table->decimal('ai_score', 5, 4)->nullable()->index();
            $table->json('ai_labels')->nullable();
            $table->string('ai_model', 120)->nullable();
            $table->decimal('ai_threshold_used', 5, 4)->nullable();
            $table->unsignedInteger('ai_analysis_time_ms')->nullable();
            $table->string('ai_action_hint', 32)->nullable()->index();
            $table->string('ai_status', 24)->default('not_requested')->index();
            $table->text('ai_advisory')->nullable();
            $table->timestamp('detected_at')->nullable()->index();
            $table->timestamp('last_scanned_at')->nullable()->index();
            $table->string('resolution_action', 32)->nullable();
            $table->text('resolution_note')->nullable();
            $table->foreignId('resolved_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('resolved_at')->nullable()->index();
            $table->timestamps();

            $table->unique('artwork_id', 'artwork_maturity_audit_findings_artwork_unique');
        });
    }

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