<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('uploads', function (Blueprint $table): void {
            $table->string('moderation_status', 16)->default('pending')->index();
            $table->timestamp('moderated_at')->nullable()->index();
            $table->foreignId('moderated_by')->nullable()->constrained('users')->nullOnDelete();
            $table->text('moderation_note')->nullable();
        });

        DB::table('uploads')
            ->where('status', 'published')
            ->update([
                'moderation_status' => 'approved',
                'updated_at' => now(),
            ]);

        DB::table('uploads')
            ->where('status', 'draft')
            ->update([
                'moderation_status' => 'pending',
                'updated_at' => now(),
            ]);
    }

    public function down(): void
    {
        Schema::table('uploads', function (Blueprint $table): void {
            $table->dropConstrainedForeignId('moderated_by');
            $table->dropColumn('moderation_note');
            $table->dropColumn('moderated_at');
            $table->dropColumn('moderation_status');
        });
    }
};
