<?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_applications', function (Blueprint $table) {
            $table->id();
            $table->string('reference_number', 50)->unique();
            $table->foreignId('member_id')->constrained()->onDelete('cascade');
            $table->foreignId('loan_product_id')->constrained()->onDelete('cascade');
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            
            // Loan Details
            $table->decimal('amount_requested', 15, 2);
            $table->decimal('amount_approved', 15, 2)->nullable();
            $table->decimal('amount_disbursed', 15, 2)->nullable();
            $table->enum('currency', ['USD', 'SSP', 'KES', 'UGX'])->default('SSP');
            $table->integer('term_months');
            $table->decimal('interest_rate', 5, 2);
            $table->text('purpose')->nullable();
            
            // Calculated Values
            $table->decimal('total_interest', 15, 2)->nullable();
            $table->decimal('total_repayable', 15, 2)->nullable();
            $table->decimal('monthly_payment', 15, 2)->nullable();
            $table->decimal('processing_fee', 15, 2)->nullable();
            
            // Status Workflow
            $table->enum('status', ['draft', 'pending', 'under_review', 'manager_approved', 'approved', 'rejected', 'disbursed', 'active', 'completed', 'defaulted', 'cancelled'])->default('draft');
            
            // Manager Review
            $table->foreignId('manager_id')->nullable()->constrained('users')->onDelete('set null');
            $table->timestamp('manager_reviewed_at')->nullable();
            $table->text('manager_review_notes')->nullable();
            $table->boolean('manager_approved')->default(false);
            
            // Super Manager Approval
            $table->foreignId('super_manager_id')->nullable()->constrained('users')->onDelete('set null');
            $table->timestamp('super_manager_approved_at')->nullable();
            $table->text('super_manager_notes')->nullable();
            $table->boolean('super_manager_approved')->default(false);
            
            // Rejection
            $table->text('rejection_reason')->nullable();
            $table->timestamp('rejected_at')->nullable();
            
            // Disbursement
            $table->timestamp('disbursed_at')->nullable();
            $table->foreignId('disbursed_by')->nullable()->constrained('users')->onDelete('set null');
            $table->string('disbursement_reference')->nullable();
            $table->string('disbursement_method')->nullable();
            
            // Repayment Tracking
            $table->decimal('amount_repaid', 15, 2)->default(0);
            $table->decimal('principal_repaid', 15, 2)->default(0);
            $table->decimal('interest_repaid', 15, 2)->default(0);
            $table->decimal('outstanding_balance', 15, 2)->nullable();
            $table->date('next_payment_date')->nullable();
            $table->decimal('next_payment_amount', 15, 2)->nullable();
            
            // Guarantors
            $table->json('guarantors')->nullable();
            
            // Collateral
            $table->json('collateral')->nullable();
            
            // Employment/Income Information
            $table->string('employment_status', 50)->nullable();
            $table->string('employer_name', 191)->nullable();
            $table->decimal('monthly_income', 15, 2)->nullable();
            $table->string('income_currency', 10)->nullable();
            
            $table->timestamps();
            $table->softDeletes();
            
            $table->index('reference_number');
            $table->index('member_id');
            $table->index('loan_product_id');
            $table->index('status');
            $table->index('created_at');
        });
    }

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