<?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_products', function (Blueprint $table) {
            $table->id();
            $table->string('name', 191);
            $table->string('slug', 191)->unique();
            $table->text('description')->nullable();
            $table->text('terms_and_conditions')->nullable();
            
            // Interest Configuration
            $table->enum('interest_type', ['fixed', 'reducing_balance', 'flat_rate'])->default('fixed');
            $table->decimal('interest_rate', 5, 2)->default(0);
            $table->string('interest_rate_display', 50)->default('per annum');
            
            // Amount Limits
            $table->decimal('min_amount', 15, 2)->default(0);
            $table->decimal('max_amount', 15, 2)->default(0);
            $table->decimal('default_amount', 15, 2)->default(0);
            $table->enum('currency', ['USD', 'SSP', 'KES', 'UGX'])->default('SSP');
            
            // Term Configuration
            $table->integer('min_term_months')->default(1);
            $table->integer('max_term_months')->default(12);
            $table->integer('default_term_months')->default(6);
            
            // Eligibility
            $table->json('eligible_categories')->nullable(); // Membership category IDs
            $table->integer('min_membership_months')->default(0);
            $table->decimal('min_contribution_balance', 15, 2)->default(0);
            $table->json('additional_requirements')->nullable();
            
            // Fees
            $table->decimal('processing_fee', 15, 2)->default(0);
            $table->decimal('processing_fee_percentage', 5, 2)->default(0);
            $table->decimal('late_payment_penalty', 5, 2)->default(0);
            
            // Status
            $table->boolean('is_active')->default(true);
            $table->boolean('is_featured')->default(false);
            $table->boolean('requires_collateral')->default(false);
            $table->boolean('requires_guarantor')->default(false);
            $table->integer('min_guarantors')->default(0);
            
            // Display
            $table->string('icon', 100)->nullable();
            $table->string('theme_color', 50)->default('default');
            $table->integer('sort_order')->default(0);
            
            $table->foreignId('created_by')->nullable()->constrained('users')->onDelete('set null');
            $table->timestamps();
            $table->softDeletes();
            
            $table->index('slug');
            $table->index('is_active');
            $table->index('is_featured');
            $table->index('sort_order');
        });
    }

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