<?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('withdrawals', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id')->comment('테넌트 ID');
            $table->date('withdrawal_date')->comment('출금일');
            $table->unsignedBigInteger('client_id')->nullable()->comment('거래처 ID');
            $table->string('client_name', 100)->nullable()->comment('비회원 거래처명');
            $table->unsignedBigInteger('bank_account_id')->nullable()->comment('출금 계좌 ID');
            $table->decimal('amount', 15, 2)->comment('금액');
            $table->string('payment_method', 20)->comment('결제수단: cash/transfer/card/check');
            $table->string('account_code', 20)->nullable()->comment('계정과목');
            $table->text('description')->nullable()->comment('적요');
            $table->string('reference_type', 50)->nullable()->comment('참조 유형: purchase/payable/payroll/etc');
            $table->unsignedBigInteger('reference_id')->nullable()->comment('참조 ID');
            $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', 'withdrawal_date'], 'idx_tenant_date');
            $table->index('client_id', 'idx_client');
            $table->index('payment_method', 'idx_payment_method');
        });
    }

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