<?php

declare(strict_types=1);

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('kr_risk_events', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('tenant_id')->index();
            $table->unsignedBigInteger('user_id')->index();
            $table->string('type', 32)->comment('事件类型');
            $table->smallInteger('score');
            $table->string('device_id', 128)->nullable();
            $table->string('fingerprint', 255)->nullable();
            $table->string('ip', 64)->nullable();
            $table->string('geo', 128)->nullable();
            $table->json('payload')->nullable();
            $table->unsignedTinyInteger('handled')->default(0);
            $table->timestamp('created_at')->useCurrent();

            $table->index(['user_id', 'created_at'], 'idx_user_time');
            $table->index(['type', 'score'], 'idx_type_score');
        });
    }

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