<?php

use App\Enums\EmployeeAdvanceStatus;
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_advances', function (Blueprint $table) {
            $table->id();

            $table->foreignId('paid_by_id')->nullable()->constrained('users')->restrictOnDelete();
            $table->foreignId('employee_id')->constrained()->cascadeOnDelete();
            $table->foreignId('verified_by_id')->nullable()->constrained('users')->restrictOnDelete();
            $table->foreignId('repayment_cash_transaction_id')->nullable()->unique()->constrained('cash_transactions')->restrictOnDelete();
            $table->foreignId('cash_transaction_id')->nullable()->unique()->constrained()->restrictOnDelete();

            $table->unsignedBigInteger('amount');
            $table->unsignedBigInteger('paid_amount')->default(0);
            $table->string('description', 100);
            $table->date('due_date');
            $table->enum('status', EmployeeAdvanceStatus::values())
                ->default(EmployeeAdvanceStatus::PENDING->value);
            $table->timestamp('verified_at')->nullable();
            $table->timestamp('paid_at')->nullable();

            $table->timestamp('created_at')->useCurrent();
            $table->timestamp('updated_at')->useCurrent()->useCurrentOnUpdate();
            $table->softDeletes();
        });
    }

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