<?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('admin_api_histories', function (Blueprint $table) {
            $table->id()->comment('ID');
            $table->foreignId('user_id')->comment('사용자 ID');
            $table->string('endpoint', 500)->comment('API 엔드포인트');
            $table->string('method', 10)->comment('HTTP 메서드');
            $table->json('request_headers')->nullable()->comment('요청 헤더');
            $table->json('request_body')->nullable()->comment('요청 본문');
            $table->integer('response_status')->comment('응답 상태 코드');
            $table->json('response_headers')->nullable()->comment('응답 헤더');
            $table->longText('response_body')->nullable()->comment('응답 본문');
            $table->integer('duration_ms')->comment('응답 시간(ms)');
            $table->string('environment', 50)->comment('실행 환경');
            $table->timestamp('created_at')->nullable();

            $table->index(['user_id', 'created_at']);
            $table->index(['endpoint', 'method']);

            $table->foreign('user_id')
                ->references('id')
                ->on('users')
                ->onDelete('cascade');
        });
    }

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