<?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('kylin_audit_logs', function (Blueprint $table) {
            $table->id();
            $table->string('event', 30)->comment('事件:created,updated,deleted');
            $table->string('model_type', 125)->comment('模型类');
            $table->unsignedBigInteger('model_id')->comment('模型ID');
            $table->json('old_values')->nullable()->comment('变更前');
            $table->json('new_values')->nullable()->comment('变更后');
            $table->unsignedBigInteger('user_id')->nullable()->comment('操作者');
            $table->string('ip', 45)->nullable();
            $table->string('user_agent', 500)->nullable();
            $table->string('url', 500)->nullable();
            $table->string('tag', 50)->nullable()->comment('标签');
            $table->unsignedBigInteger('tenant_id')->nullable()->comment('租户ID（多租户隔离）');
            $table->timestamps();
            $table->index(['model_type', 'model_id']);
            $table->index('user_id');
            $table->index('created_at');
            $table->index('tenant_id');
        });
    }

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