<?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
    {
        if (! Schema::hasTable('activity_logs')) {
            Schema::create('activity_logs', function (Blueprint $table) {
                $table->id();

                // This adds columns + an index already:
                //   loggable_type, loggable_id, and an index on (loggable_type, loggable_id)
                $table->morphs('loggable');

                $table->unsignedBigInteger('user_id')->nullable()->index();

                $table->string('action', 50);
                $table->string('field', 100)->nullable();
                $table->text('from')->nullable();
                $table->text('to')->nullable();
                $table->json('meta')->nullable();

                $table->timestamps();

                // Keep this:
                $table->index('created_at');

                // ❌ REMOVE this line (it duplicates the morphs index)
                // $table->index(['loggable_type', 'loggable_id']);
            });
        }
    }

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