<?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('ifta_questions', function (Blueprint $table): void {
            $table->boolean('is_approved')->default(false)->after('status')->index();
            $table->timestamp('approved_at')->nullable()->after('answered_at');
            $table->foreignId('approved_by')->nullable()->after('approved_at')->constrained('users')->nullOnDelete();
        });

        Schema::table('ifta_replies', function (Blueprint $table): void {
            $table->boolean('is_approved')->default(false)->after('is_official')->index();
            $table->timestamp('approved_at')->nullable()->after('is_approved');
            $table->foreignId('approved_by')->nullable()->after('approved_at')->constrained('users')->nullOnDelete();
        });

        DB::table('ifta_questions')->update([
            'is_approved' => true,
            'approved_at' => now(),
        ]);

        DB::table('ifta_replies')->update([
            'is_approved' => true,
            'approved_at' => now(),
        ]);
    }

    public function down(): void
    {
        Schema::table('ifta_replies', function (Blueprint $table): void {
            $table->dropConstrainedForeignId('approved_by');
            $table->dropColumn(['is_approved', 'approved_at']);
        });

        Schema::table('ifta_questions', function (Blueprint $table): void {
            $table->dropConstrainedForeignId('approved_by');
            $table->dropColumn(['is_approved', 'approved_at']);
        });
    }
};
