<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Modules\Payroll\Enums\EmployeeLoansEnum;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('employee_loans', function (Blueprint $table) {
            $table->id();
            $table->foreignId('employee_id')->constrained('employees')->cascadeOnDelete();
            $table->foreignId('voucher_id')->nullable()->constrained('account_vouchers')->nullOnDelete();
            $table->string('loan_no')->unique();
            $table->string('loan_type')->nullable();
            $table->decimal('principal_amount', 12, 2);
            $table->decimal('interest_rate', 5, 2)->default(0);
            $table->integer('installment_months');
            $table->decimal('interest_amount', 12, 2)->default(0);
            $table->decimal('total_payable', 12, 2);
            $table->decimal('remaining_amount', 12, 2);
            $table->decimal('monthly_installment', 12, 2);
            $table->integer('paid_installments')->default(0);
            $table->date('issue_date');
            $table->date('start_deduction_date');
            $table->enum('status', EmployeeLoansEnum::casesAsArray())->default(EmployeeLoansEnum::PENDING);
            $table->text('remarks')->nullable();
            $table->foreignId('approved_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('approved_at')->nullable();
            $table->timestamps();
        });
    }

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