<?php

namespace FleekDash\Database\Migrations;

use FleekDash\Database\Core\BaseMigration;
use FleekDash\Database\Core\TableBuilder;

defined('ABSPATH') || exit;

return new class extends BaseMigration {
    public function up(): void
    {
        $this->createTable('fleekdash_realtime_event_outbox', function (TableBuilder $table) {
            $table->id();
            $table->string('event_id', 64);
            $table->string('event_type', 80);
            $table->string('tenant_id', 120)->nullable();
            $table->unsignedBigInteger('user_id')->nullable();
            $table->string('scope', 20)->default('all');
            $table->longText('payload_min');
            $table->string('status', 20)->default('pending');
            $table->integer('attempts')->default(0);
            $table->text('last_error')->nullable();
            $table->datetime('available_at');
            $table->datetime('dispatched_at')->nullable();
            $table->timestamps();

            $table->unique('event_id', 'uniq_event_id');
            $table->index(['status', 'available_at'], 'idx_status_available');
            $table->index('event_type', 'idx_event_type');
            $table->index('user_id', 'idx_user_id');
            $table->index('tenant_id', 'idx_tenant_id');
        });
    }

    public function down(): void
    {
        $this->dropTable('fleekdash_realtime_event_outbox');
    }
};

