<?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('tag_interaction_events', function (Blueprint $table): void {
            $table->id();
            $table->date('event_date')->index();
            $table->string('event_type', 24)->index();
            $table->string('surface', 32)->index();
            $table->foreignId('user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->string('session_key', 64)->nullable()->index();
            $table->string('tag_slug', 120)->nullable()->index();
            $table->string('source_tag_slug', 120)->nullable()->index();
            $table->string('query', 120)->nullable();
            $table->unsignedSmallInteger('position')->nullable();
            $table->json('meta')->nullable();
            $table->timestamp('occurred_at')->nullable()->index();
            $table->timestamps();

            $table->index(['event_date', 'surface', 'event_type'], 'tag_interaction_events_daily_idx');
            $table->index(['tag_slug', 'surface', 'event_date'], 'tag_interaction_events_tag_surface_idx');
        });
    }

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