<?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('event_join_token_events', function (Blueprint $table) {
            $table->id();
            $table->foreignId('event_join_token_id')->nullable()->constrained()->nullOnDelete();
            $table->unsignedBigInteger('event_id')->nullable()->index();
            $table->unsignedBigInteger('tenant_id')->nullable()->index();
            $table->string('token_hash', 64)->nullable()->index();
            $table->string('token_preview', 32)->nullable();
            $table->string('event_type', 32);
            $table->string('route', 100)->nullable()->index();
            $table->string('http_method', 16)->nullable();
            $table->unsignedSmallInteger('http_status')->nullable();
            $table->string('device_id', 64)->nullable();
            $table->string('ip_address', 45)->nullable();
            $table->text('user_agent')->nullable();
            $table->json('context')->nullable();
            $table->timestamp('occurred_at')->useCurrent();
            $table->timestamps();

            $table->index(['event_join_token_id', 'occurred_at'], 'event_join_token_events_token_time_idx');
        });
    }

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

