<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    protected $connection = 'sam_stat';

    public function up(): void
    {
        Schema::connection('sam_stat')->create('stat_events', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id');
            $table->string('domain', 50);
            $table->string('event_type', 100);
            $table->string('entity_type', 100);
            $table->unsignedBigInteger('entity_id');
            $table->json('payload')->nullable();
            $table->timestamp('occurred_at');

            $table->index(['tenant_id', 'domain'], 'idx_tenant_domain');
            $table->index('occurred_at', 'idx_occurred');
            $table->index(['entity_type', 'entity_id'], 'idx_entity');
        });
    }

    public function down(): void
    {
        Schema::connection('sam_stat')->dropIfExists('stat_events');
    }
};
