<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('settlement_batches', function (Blueprint $table): void {
            if (! Schema::hasColumn('settlement_batches', 'review_status')) {
                $table->string('review_status', 32)->default('pending')->after('total_jackpot_payout_amount');
            }
            if (! Schema::hasColumn('settlement_batches', 'reviewed_by')) {
                $table->foreignId('reviewed_by')->nullable()->after('review_status')->constrained('admin_users')->nullOnDelete();
            }
            if (! Schema::hasColumn('settlement_batches', 'reviewed_at')) {
                $table->timestamp('reviewed_at')->nullable()->after('reviewed_by');
            }
            if (! Schema::hasColumn('settlement_batches', 'review_remark')) {
                $table->string('review_remark', 255)->nullable()->after('reviewed_at');
            }
            if (! Schema::hasColumn('settlement_batches', 'paid_at')) {
                $table->timestamp('paid_at')->nullable()->after('review_remark');
            }
        });
    }

    public function down(): void
    {
        Schema::table('settlement_batches', function (Blueprint $table): void {
            if (Schema::hasColumn('settlement_batches', 'paid_at')) {
                $table->dropColumn('paid_at');
            }
            if (Schema::hasColumn('settlement_batches', 'review_remark')) {
                $table->dropColumn('review_remark');
            }
            if (Schema::hasColumn('settlement_batches', 'reviewed_at')) {
                $table->dropColumn('reviewed_at');
            }
            if (Schema::hasColumn('settlement_batches', 'reviewed_by')) {
                $table->dropForeign(['reviewed_by']);
                $table->dropColumn('reviewed_by');
            }
            if (Schema::hasColumn('settlement_batches', 'review_status')) {
                $table->dropColumn('review_status');
            }
        });
    }
};
