<?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::create('loan_products', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('business_id')->constrained()->cascadeOnDelete();
            $table->string('name');
            $table->decimal('minimum_amount', 18, 2);
            $table->decimal('maximum_amount', 18, 2);
            $table->unsignedInteger('duration');
            $table->string('duration_unit')->default('months');
            $table->decimal('interest_rate', 12, 6)->default(0);
            $table->string('interest_calculation_method')->default('flat');
            $table->decimal('fixed_interest_amount', 18, 2)->nullable();
            $table->string('repayment_frequency')->default('monthly');
            $table->unsignedInteger('grace_period_days')->default(0);
            $table->string('late_payment_penalty_type')->default('none');
            $table->decimal('late_payment_penalty_value', 18, 6)->default(0);
            $table->string('processing_fee_type')->default('fixed');
            $table->decimal('processing_fee_value', 18, 6)->default(0);
            $table->string('insurance_fee_type')->default('fixed');
            $table->decimal('insurance_fee_value', 18, 6)->default(0);
            $table->decimal('other_charges', 18, 2)->default(0);
            $table->boolean('collateral_required')->default(false);
            $table->boolean('guarantor_required')->default(false);
            $table->string('status')->default('active');
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->unique(['business_id', 'name']);
            $table->index(['business_id', 'status']);
        });
    }

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