<?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('businesses', function (Blueprint $table): void {
            $table->decimal('top_up_eligibility_percentage', 5, 2)->default(70)->after('manager_approval_threshold');
        });

        Schema::table('loans', function (Blueprint $table): void {
            $table->foreignId('top_up_source_loan_id')->nullable()->after('loan_product_id')->constrained('loans')->restrictOnDelete();
            $table->decimal('top_up_eligibility_percentage', 5, 2)->nullable()->after('top_up_source_loan_id');
            $table->decimal('top_up_paid_percentage_at_application', 5, 2)->nullable()->after('top_up_eligibility_percentage');
            $table->boolean('top_up_override_used')->default(false)->after('top_up_paid_percentage_at_application');
            $table->text('top_up_override_reason')->nullable()->after('top_up_override_used');
            $table->foreignId('top_up_override_by')->nullable()->after('top_up_override_reason')->constrained('users')->nullOnDelete();
            $table->timestamp('top_up_override_at')->nullable()->after('top_up_override_by');
            $table->index(['top_up_source_loan_id', 'status']);
        });

        Schema::create('loan_top_ups', function (Blueprint $table): void {
            $table->id();
            $table->string('top_up_number')->unique();
            $table->foreignId('branch_id')->constrained()->restrictOnDelete();
            $table->foreignId('previous_loan_id')->unique()->constrained('loans')->restrictOnDelete();
            $table->foreignId('new_loan_id')->unique()->constrained('loans')->restrictOnDelete();
            $table->foreignId('financial_account_id')->constrained('financial_accounts')->restrictOnDelete();
            $table->decimal('previous_outstanding', 18, 2);
            $table->decimal('previous_principal', 18, 2);
            $table->decimal('previous_interest', 18, 2);
            $table->decimal('previous_fees', 18, 2);
            $table->decimal('previous_penalties', 18, 2);
            $table->decimal('amount_settled_internally', 18, 2);
            $table->decimal('new_principal', 18, 2);
            $table->decimal('new_interest', 18, 2);
            $table->decimal('new_fees', 18, 2);
            $table->decimal('new_total_repayment', 18, 2);
            $table->decimal('cash_given_to_customer', 18, 2);
            $table->date('top_up_date');
            $table->string('reference')->nullable();
            $table->text('notes')->nullable();
            $table->foreignId('executed_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('financial_account_transaction_id')->nullable()->constrained('financial_account_transactions')->nullOnDelete();
            $table->timestamps();

            $table->index(['branch_id', 'top_up_date']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('loan_top_ups');

        Schema::table('loans', function (Blueprint $table): void {
            $table->dropForeign(['top_up_source_loan_id']);
            $table->dropForeign(['top_up_override_by']);
            $table->dropIndex(['top_up_source_loan_id', 'status']);
            $table->dropColumn([
                'top_up_source_loan_id', 'top_up_eligibility_percentage', 'top_up_paid_percentage_at_application',
                'top_up_override_used', 'top_up_override_reason', 'top_up_override_by', 'top_up_override_at',
            ]);
        });

        Schema::table('businesses', function (Blueprint $table): void {
            $table->dropColumn('top_up_eligibility_percentage');
        });
    }
};
