<?php

declare(strict_types=1);

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

/**
 * AI Biography v1.1 metadata columns.
 *
 * Adds:
 *   prompt_version       — which prompt template was used (e.g. "v1.1")
 *   input_quality_tier   — classified creator data richness: rich|medium|sparse
 *   generation_reason    — why generation was triggered (e.g. "manual_regenerate")
 *   needs_review         — flagged for admin inspection (poor output, low-signal, etc.)
 *   last_attempted_at    — timestamp of the most recent generation attempt
 *   last_error_code      — short machine-readable error code (e.g. "validation_failed")
 *   last_error_reason    — human-readable failure description from the last attempt
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::table('creator_ai_biographies', function (Blueprint $table): void {
            $table->string('prompt_version', 20)->nullable()->after('model');
            $table->string('input_quality_tier', 20)->nullable()->after('prompt_version');
            $table->string('generation_reason', 50)->nullable()->after('input_quality_tier');
            $table->boolean('needs_review')->default(false)->after('is_user_edited');
            $table->timestamp('last_attempted_at')->nullable()->after('approved_at');
            $table->string('last_error_code', 30)->nullable()->after('last_attempted_at');
            $table->text('last_error_reason')->nullable()->after('last_error_code');
        });
    }

    public function down(): void
    {
        Schema::table('creator_ai_biographies', function (Blueprint $table): void {
            $table->dropColumn([
                'prompt_version',
                'input_quality_tier',
                'generation_reason',
                'needs_review',
                'last_attempted_at',
                'last_error_code',
                'last_error_reason',
            ]);
        });
    }
};
