<?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('expected_expenses', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id')->comment('테넌트 ID');
            $table->date('expected_payment_date')->comment('예상 지급일');
            $table->date('settlement_date')->nullable()->comment('결제일');
            $table->string('transaction_type', 30)->comment('거래유형: purchase/advance/suspense/rent/salary/insurance/tax/utilities/other');
            $table->decimal('amount', 15, 2)->comment('지출금액');
            $table->unsignedBigInteger('client_id')->nullable()->comment('거래처 ID');
            $table->string('client_name', 100)->nullable()->comment('비회원 거래처명');
            $table->unsignedBigInteger('bank_account_id')->nullable()->comment('계좌 ID');
            $table->string('account_code', 50)->nullable()->comment('계정과목');
            $table->string('payment_status', 20)->default('pending')->comment('지급상태: pending/partial/paid/overdue');
            $table->string('approval_status', 20)->default('none')->comment('결재상태: none/pending/approved/rejected');
            $table->text('description')->nullable()->comment('적요/메모');
            $table->unsignedBigInteger('created_by')->nullable()->comment('생성자 ID');
            $table->unsignedBigInteger('updated_by')->nullable()->comment('수정자 ID');
            $table->unsignedBigInteger('deleted_by')->nullable()->comment('삭제자 ID');
            $table->softDeletes();
            $table->timestamps();

            $table->index(['tenant_id', 'expected_payment_date'], 'idx_tenant_expected_date');
            $table->index(['tenant_id', 'payment_status'], 'idx_tenant_payment_status');
            $table->index('client_id', 'idx_client');
            $table->index('transaction_type', 'idx_transaction_type');
        });
    }

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