<?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('artworks')) {
            return;
        }

        Schema::table('artworks', function (Blueprint $table): void {
            if (! Schema::hasColumn('artworks', 'maturity_ai_label')) {
                $table->string('maturity_ai_label', 24)->nullable()->index();
            }
            if (! Schema::hasColumn('artworks', 'maturity_ai_confidence')) {
                $table->decimal('maturity_ai_confidence', 5, 4)->nullable()->index();
            }
            if (! Schema::hasColumn('artworks', 'maturity_ai_model')) {
                $table->string('maturity_ai_model', 120)->nullable();
            }
            if (! Schema::hasColumn('artworks', 'maturity_ai_threshold_used')) {
                $table->decimal('maturity_ai_threshold_used', 5, 4)->nullable();
            }
            if (! Schema::hasColumn('artworks', 'maturity_ai_analysis_time_ms')) {
                $table->unsignedInteger('maturity_ai_analysis_time_ms')->nullable();
            }
            if (! Schema::hasColumn('artworks', 'maturity_ai_action_hint')) {
                $table->string('maturity_ai_action_hint', 32)->nullable()->index();
            }
            if (! Schema::hasColumn('artworks', 'maturity_ai_advisory')) {
                $table->text('maturity_ai_advisory')->nullable();
            }
            if (! Schema::hasColumn('artworks', 'maturity_ai_status')) {
                $table->string('maturity_ai_status', 24)->default('not_requested')->index();
            }
            if (! Schema::hasColumn('artworks', 'maturity_declared_at')) {
                $table->timestamp('maturity_declared_at')->nullable()->index();
            }
        });
    }

    public function down(): void
    {
        if (! Schema::hasTable('artworks')) {
            return;
        }

        Schema::table('artworks', function (Blueprint $table): void {
            foreach ([
                'maturity_ai_label',
                'maturity_ai_confidence',
                'maturity_ai_model',
                'maturity_ai_threshold_used',
                'maturity_ai_analysis_time_ms',
                'maturity_ai_action_hint',
                'maturity_ai_advisory',
                'maturity_ai_status',
                'maturity_declared_at',
            ] as $column) {
                if (Schema::hasColumn('artworks', $column)) {
                    $table->dropColumn($column);
                }
            }
        });
    }
};