<?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
    {
        if (Schema::hasTable('card_transactions')) {
            return;
        }
        Schema::create('card_transactions', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id');
            $table->unsignedBigInteger('card_id')->nullable();   // corporate_cards.id
            $table->date('transaction_date');                     // 거래일
            $table->string('time', 10)->nullable();              // 시간 (HH:MM)
            $table->string('merchant', 200);                     // 가맹점명
            $table->string('category', 50)->nullable();          // 카테고리
            $table->bigInteger('amount')->default(0);            // 금액 (음수=취소)
            $table->string('approval_no', 50)->nullable();       // 승인번호
            $table->string('status', 20)->default('approved');   // approved, cancelled
            $table->text('memo')->nullable();
            $table->timestamps();
            $table->softDeletes();
            $table->index(['tenant_id', 'transaction_date']);
            $table->index(['tenant_id', 'card_id']);
        });
    }

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