<?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('api_request_logs', function (Blueprint $table) {
            $table->id();
            $table->string('method', 10)->comment('HTTP 메서드');
            $table->string('url', 2048)->comment('요청 URL');
            $table->string('route_name')->nullable()->comment('라우트 이름');
            $table->json('request_headers')->nullable()->comment('요청 헤더');
            $table->json('request_body')->nullable()->comment('요청 바디');
            $table->json('request_query')->nullable()->comment('쿼리 파라미터');
            $table->unsignedSmallInteger('response_status')->comment('응답 상태 코드');
            $table->longText('response_body')->nullable()->comment('응답 바디');
            $table->unsignedInteger('duration_ms')->comment('처리 시간 (ms)');
            $table->string('ip_address', 45)->nullable()->comment('클라이언트 IP');
            $table->string('user_agent', 512)->nullable()->comment('User Agent');
            $table->unsignedBigInteger('user_id')->nullable()->comment('사용자 ID');
            $table->unsignedBigInteger('tenant_id')->nullable()->comment('테넌트 ID');
            $table->timestamp('created_at')->useCurrent()->comment('생성일시');

            $table->index('created_at');
            $table->index(['method', 'response_status']);
            $table->index('user_id');
            $table->index('tenant_id');
        });
    }

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