<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('payment_events', function (Blueprint $table) {
            $table->id();
            $table->string('event_type', 100);
            $table->string('level', 20)->default('info');
            $table->json('data');
            $table->nullableMorphs('user');
            $table->string('request_id')->nullable();
            $table->string('ip_address', 45)->nullable();
            $table->text('user_agent')->nullable();
            $table->timestamp('expires_at')->nullable();
            $table->timestamps();

            // Indexes for performance
            $table->index(['event_type', 'created_at']);
            $table->index(['user_type', 'user_id', 'created_at']);
            $table->index(['level', 'created_at']);
            $table->index('expires_at');
            $table->index('request_id');

            // Full-text index for searching data (if supported)
            if (config('database.default') === 'mysql') {
                $table->fullText('data');
            }
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('payment_events');
    }
};
