<?php

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

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

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