<?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('user_activities', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
            $table->string('type', 32);
            $table->string('entity_type', 48);
            $table->unsignedBigInteger('entity_id');
            $table->json('meta')->nullable();
            $table->timestamp('created_at')->useCurrent();

            $table->index(['user_id', 'created_at'], 'user_activities_user_created_idx');
            $table->index(['user_id', 'type', 'created_at'], 'user_activities_user_type_created_idx');
            $table->index(['entity_type', 'entity_id'], 'user_activities_entity_idx');
        });
    }

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