<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('loan_repayments', function (Blueprint $table) {
            $table->id();
            $table->string('reference_number', 50)->unique();
            $table->foreignId('loan_application_id')->constrained()->onDelete('cascade');
            $table->foreignId('member_id')->constrained()->onDelete('cascade');
            
            // Payment Schedule
            $table->integer('installment_number');
            $table->date('due_date');
            $table->decimal('scheduled_amount', 15, 2);
            $table->decimal('scheduled_principal', 15, 2);
            $table->decimal('scheduled_interest', 15, 2);
            
            // Actual Payment
            $table->decimal('amount_paid', 15, 2)->default(0);
            $table->decimal('principal_paid', 15, 2)->default(0);
            $table->decimal('interest_paid', 15, 2)->default(0);
            $table->decimal('penalty_paid', 15, 2)->default(0);
            $table->decimal('extra_payment', 15, 2)->default(0);
            
            // Status
            $table->enum('status', ['scheduled', 'pending', 'paid', 'partial', 'overdue', 'cancelled'])->default('scheduled');
            $table->date('paid_date')->nullable();
            
            // Payment Details
            $table->string('payment_method')->nullable();
            $table->string('payment_reference')->nullable();
            $table->string('transaction_id')->nullable();
            $table->json('payment_details')->nullable();
            
            // Penalty
            $table->decimal('penalty_amount', 15, 2)->default(0);
            $table->boolean('penalty_applied')->default(false);
            $table->timestamp('penalty_applied_at')->nullable();
            
            // Outstanding Balance After Payment
            $table->decimal('principal_balance', 15, 2)->nullable();
            
            $table->text('notes')->nullable();
            $table->timestamps();
            
            $table->index('reference_number');
            $table->index('loan_application_id');
            $table->index('member_id');
            $table->index('status');
            $table->index('due_date');
            $table->index('installment_number');
            $table->index(['loan_application_id', 'status']);
        });

        // Loan amortization schedule
        Schema::create('loan_amortization_schedules', function (Blueprint $table) {
            $table->id();
            $table->foreignId('loan_application_id')->constrained()->onDelete('cascade');
            
            $table->integer('installment_number');
            $table->date('payment_date');
            $table->decimal('beginning_balance', 15, 2);
            $table->decimal('scheduled_payment', 15, 2);
            $table->decimal('principal', 15, 2);
            $table->decimal('interest', 15, 2);
            $table->decimal('ending_balance', 15, 2);
            
            $table->timestamps();
            
            $table->index('loan_application_id');
            $table->unique(['loan_application_id', 'installment_number'], 'loan_amort_unique');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('loan_amortization_schedules');
        Schema::dropIfExists('loan_repayments');
    }
};
