<?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('loan_repayments', function (Blueprint $table) {
            if (!Schema::hasColumn('loan_repayments', 'bank_account_id')) {
                $table->unsignedBigInteger('bank_account_id')->nullable()->after('total_amount');
                $table->foreign('bank_account_id')->references('id')->on('bank_accounts')->onDelete('set null');
            }
            if (!Schema::hasColumn('loan_repayments', 'super_admin_bank_account_id')) {
                $table->unsignedBigInteger('super_admin_bank_account_id')->nullable()->after('bank_account_id');
                $table->foreign('super_admin_bank_account_id')->references('id')->on('super_admin_bank_accounts')->onDelete('set null');
            }
        });
    }

    public function down(): void
    {
        Schema::table('loan_repayments', function (Blueprint $table) {
            if (Schema::hasColumn('loan_repayments', 'super_admin_bank_account_id')) {
                $table->dropForeign(['super_admin_bank_account_id']);
                $table->dropColumn('super_admin_bank_account_id');
            }
            if (Schema::hasColumn('loan_repayments', 'bank_account_id')) {
                $table->dropForeign(['bank_account_id']);
                $table->dropColumn('bank_account_id');
            }
        });
    }
};
