<?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('expense_accounts', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id')->comment('테넌트 ID');

            // 비용 유형
            $table->string('account_type', 50)->comment('계정 유형: welfare, entertainment, etc.');
            $table->string('sub_type', 50)->nullable()->comment('세부 유형: meal, gift, etc.');

            // 비용 정보
            $table->date('expense_date')->comment('지출일');
            $table->decimal('amount', 15, 2)->default(0)->comment('금액');
            $table->string('description', 500)->nullable()->comment('비용 내역');
            $table->string('receipt_no', 100)->nullable()->comment('증빙번호');

            // 거래처 정보 (접대비 등에서 사용)
            $table->unsignedBigInteger('vendor_id')->nullable()->comment('거래처 ID');
            $table->string('vendor_name', 200)->nullable()->comment('거래처명 (직접 입력)');

            // 카드/결제 정보
            $table->string('payment_method', 50)->nullable()->comment('결제수단: card, cash, transfer');
            $table->string('card_no', 50)->nullable()->comment('카드 마지막 4자리');

            // 감사 컬럼
            $table->unsignedBigInteger('created_by')->nullable()->comment('등록자');
            $table->unsignedBigInteger('updated_by')->nullable()->comment('수정자');
            $table->unsignedBigInteger('deleted_by')->nullable()->comment('삭제자');

            $table->timestamps();
            $table->softDeletes();

            // 인덱스
            $table->index(['tenant_id', 'account_type', 'expense_date']);
            $table->index(['tenant_id', 'expense_date']);

            // 외래키
            $table->foreign('tenant_id')->references('id')->on('tenants')->onDelete('cascade');
            $table->foreign('vendor_id')->references('id')->on('clients')->onDelete('set null');
        });
    }

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