<?php
// database/migrations/xxxx_xx_xx_create_payroll_loans_table.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('loans', function (Blueprint $table) {
            $table->id();
            $table->foreignId('branch_id')->nullable();
            $table->string('code')->unique();
            $table->string('name');
            $table->text('description')->nullable();

            $table->decimal('min_amount', 12, 2)->nullable();
            $table->decimal('max_amount', 12, 2)->nullable();

            $table->unsignedSmallInteger('min_installments')->nullable();
            $table->unsignedSmallInteger('max_installments')->nullable();

            $table->enum('interest_type', ['NONE', 'FIXED', 'REDUCING'])->default('NONE');
            $table->decimal('interest_rate', 5, 2)->nullable();

            $table->enum('recovery_start', ['IMMEDIATE', 'NEXT_MONTH'])->default('NEXT_MONTH');
            $table->enum('recovery_frequency', ['MONTHLY', 'BIWEEKLY'])->default('MONTHLY');
            $table->boolean('allow_partial_recovery')->default(false);

            $table->string('deduction_code')->nullable();

            $table->boolean('requires_approval')->default(false);
            $table->unsignedSmallInteger('approval_levels')->nullable();

            $table->unsignedSmallInteger('sort_order')->default(0);
            $table->date('effective_from')->nullable();
            $table->date('effective_to')->nullable();

            $table->boolean('status')->default(true);
            $table->timestamps();
        });
    }

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