<?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('employee_loans', function (Blueprint $table) {
            $table->id();
            $table->bigInteger('employee_id')->unsigned();
            $table->string('loan_number', 50)->unique();
            $table->decimal('loan_amount', 15, 2)->comment('Total kasbon awal');
            $table->decimal('installment_amount', 15, 2)->comment('Nominal potong per slip gaji');
            $table->decimal('remaining_balance', 15, 2)->comment('Sisa piutang karyawan (real-time)');
            $table->enum('status', ['active', 'paid_off', 'void'])->default('active');
            $table->bigInteger('created_by')->unsigned()->comment('User ID (HRD) yang menginput');
            $table->timestamps();

            $table->foreign('employee_id')->references('id')->on('employees')->onDelete('cascade');
            $table->foreign('created_by')->references('id')->on('users');
        });
    }

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