<?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_level')) {
                $table->string('maturity_level', 24)->default('safe')->index();
            }
            if (! Schema::hasColumn('artworks', 'maturity_source')) {
                $table->string('maturity_source', 24)->default('legacy')->index();
            }
            if (! Schema::hasColumn('artworks', 'maturity_status')) {
                $table->string('maturity_status', 24)->default('clear')->index();
            }
            if (! Schema::hasColumn('artworks', 'maturity_ai_score')) {
                $table->decimal('maturity_ai_score', 5, 4)->nullable()->index();
            }
            if (! Schema::hasColumn('artworks', 'maturity_ai_labels')) {
                $table->json('maturity_ai_labels')->nullable();
            }
            if (! Schema::hasColumn('artworks', 'maturity_ai_detected_at')) {
                $table->timestamp('maturity_ai_detected_at')->nullable()->index();
            }
            if (! Schema::hasColumn('artworks', 'maturity_flagged_at')) {
                $table->timestamp('maturity_flagged_at')->nullable()->index();
            }
            if (! Schema::hasColumn('artworks', 'maturity_flag_reason')) {
                $table->string('maturity_flag_reason', 255)->nullable();
            }
            if (! Schema::hasColumn('artworks', 'maturity_reviewed_by')) {
                $table->unsignedBigInteger('maturity_reviewed_by')->nullable()->index();
            }
            if (! Schema::hasColumn('artworks', 'maturity_reviewed_at')) {
                $table->timestamp('maturity_reviewed_at')->nullable()->index();
            }
            if (! Schema::hasColumn('artworks', 'maturity_reviewer_note')) {
                $table->text('maturity_reviewer_note')->nullable();
            }
            if (! Schema::hasColumn('artworks', 'maturity_mismatch_count')) {
                $table->unsignedInteger('maturity_mismatch_count')->default(0);
            }
        });
    }

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

        Schema::table('artworks', function (Blueprint $table): void {
            foreach ([
                'maturity_level',
                'maturity_source',
                'maturity_status',
                'maturity_ai_score',
                'maturity_ai_labels',
                'maturity_ai_detected_at',
                'maturity_flagged_at',
                'maturity_flag_reason',
                'maturity_reviewed_by',
                'maturity_reviewed_at',
                'maturity_reviewer_note',
                'maturity_mismatch_count',
            ] as $column) {
                if (Schema::hasColumn('artworks', $column)) {
                    $table->dropColumn($column);
                }
            }
        });
    }
};