<?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('loans', function (Blueprint $table): void {
            $table->string('auto_debit_mode', 50)->default('manual_only')->after('processing_fee_timing');
            $table->foreignId('auto_debit_savings_account_id')->nullable()->after('auto_debit_mode')->constrained('savings_accounts')->nullOnDelete();
            $table->timestamp('auto_debit_authorized_at')->nullable()->after('auto_debit_savings_account_id');
            $table->timestamp('auto_debit_last_attempt_at')->nullable()->after('auto_debit_authorized_at');
            $table->text('auto_debit_failure_reason')->nullable()->after('auto_debit_last_attempt_at');
            $table->index(['tenant_id', 'auto_debit_mode']);
        });

        Schema::table('savings_accounts', function (Blueprint $table): void {
            $table->string('auto_debit_mode', 50)->default('manual_only')->after('processing_fee_timing');
            $table->foreignId('auto_debit_savings_account_id')->nullable()->after('auto_debit_mode')->constrained('savings_accounts')->nullOnDelete();
            $table->timestamp('auto_debit_authorized_at')->nullable()->after('auto_debit_savings_account_id');
            $table->timestamp('auto_debit_last_attempt_at')->nullable()->after('auto_debit_authorized_at');
            $table->text('auto_debit_failure_reason')->nullable()->after('auto_debit_last_attempt_at');
            $table->index(['tenant_id', 'auto_debit_mode']);
        });

        Schema::table('bill_payments', function (Blueprint $table): void {
            $table->foreignId('savings_transaction_id')->nullable()->after('recorded_by')->constrained('savings_transactions')->nullOnDelete();
            $table->boolean('initiated_by_member')->default(false)->after('savings_transaction_id');
            $table->json('provider_response')->nullable()->after('notes');
            $table->index(['tenant_id', 'initiated_by_member', 'status']);
        });
    }

    public function down(): void
    {
        Schema::table('bill_payments', function (Blueprint $table): void {
            $table->dropIndex(['tenant_id', 'initiated_by_member', 'status']);
            $table->dropConstrainedForeignId('savings_transaction_id');
            $table->dropColumn(['initiated_by_member', 'provider_response']);
        });

        Schema::table('savings_accounts', function (Blueprint $table): void {
            $table->dropIndex(['tenant_id', 'auto_debit_mode']);
            $table->dropConstrainedForeignId('auto_debit_savings_account_id');
            $table->dropColumn([
                'auto_debit_mode',
                'auto_debit_authorized_at',
                'auto_debit_last_attempt_at',
                'auto_debit_failure_reason',
            ]);
        });

        Schema::table('loans', function (Blueprint $table): void {
            $table->dropIndex(['tenant_id', 'auto_debit_mode']);
            $table->dropConstrainedForeignId('auto_debit_savings_account_id');
            $table->dropColumn([
                'auto_debit_mode',
                'auto_debit_authorized_at',
                'auto_debit_last_attempt_at',
                'auto_debit_failure_reason',
            ]);
        });
    }
};
