<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('committee_reports', function (Blueprint $table) {
            $table->string('status', 20)->default('pending')->after('file_path'); // pending, approved, rejected
            $table->foreignId('reviewed_by')->nullable()->constrained('users')->after('status');
            $table->timestamp('reviewed_at')->nullable()->after('reviewed_by');
            $table->string('rejection_reason')->nullable()->after('reviewed_at');

            $table->index(['committee_id', 'status']);
        });

        // Auto-approve all existing reports (they were admin-created)
        DB::table('committee_reports')->update(['status' => 'approved']);
    }

    public function down(): void
    {
        Schema::table('committee_reports', function (Blueprint $table) {
            $table->dropIndex(['committee_id', 'status']);
            $table->dropConstrainedForeignId('reviewed_by');
            $table->dropColumn(['status', 'reviewed_at', 'rejection_reason']);
        });
    }
};
