<?php

use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Migrations\Migration;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('api_request_logs', function (Blueprint $table) {
            $table->id()->comment('主键');
            $table->unsignedBigInteger('user_id')->nullable()->comment('请求用户 ID');
            $table->string('method', 10)->comment('HTTP 方法');
            $table->string('path', 500)->comment('请求路径');
            $table->smallInteger('status_code')->comment('HTTP 状态码');
            $table->string('ip', 45)->nullable()->comment('客户端 IP');
            $table->string('user_agent', 500)->nullable()->comment('User-Agent');
            $table->jsonb('request_body')->nullable()->comment('请求体（脱敏后）');
            $table->integer('response_code')->nullable()->comment('业务响应码');
            $table->integer('duration_ms')->comment('请求耗时（毫秒）');
            $table->timestampTz('created_at')->useCurrent()->comment('创建时间');

            $table->index('user_id');
            $table->index('path');
            $table->index('created_at');
            $table->index('status_code');
        });
    }

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