<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('loans', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id')->comment('테넌트 ID');
            $table->unsignedBigInteger('user_id')->comment('가지급금 수령자');
            $table->date('loan_date')->comment('지급일');
            $table->decimal('amount', 15, 2)->comment('가지급금액');
            $table->text('purpose')->nullable()->comment('사용목적');
            $table->date('settlement_date')->nullable()->comment('정산일');
            $table->decimal('settlement_amount', 15, 2)->nullable()->comment('정산금액');
            $table->string('status', 20)->default('outstanding')->comment('상태: outstanding/settled/partial');
            $table->unsignedBigInteger('withdrawal_id')->nullable()->comment('출금 연결');
            $table->unsignedBigInteger('created_by')->nullable()->comment('생성자');
            $table->unsignedBigInteger('updated_by')->nullable()->comment('수정자');
            $table->unsignedBigInteger('deleted_by')->nullable()->comment('삭제자');
            $table->softDeletes();
            $table->timestamps();

            $table->index(['tenant_id', 'user_id'], 'idx_tenant_user');
            $table->index('status', 'idx_status');
            $table->index(['tenant_id', 'loan_date'], 'idx_tenant_date');
        });
    }

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