<?php

use Openguru\OpenCartFramework\Migrations\Migration;

return new class extends Migration {
    public function up(): void
    {
        $tableName = 'telecart_events';

        $sql = <<<SQL
            CREATE TABLE IF NOT EXISTS `{$tableName}` (
                `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
                `event` VARCHAR(255) NOT NULL,
                `payload` TEXT NOT NULL,
                `idempotency_key` VARCHAR(64) NOT NULL,
                `event_time` DATETIME NOT NULL,
                `status` VARCHAR(50) NOT NULL DEFAULT 'pending',
                `attempts_count` INT(11) UNSIGNED NOT NULL DEFAULT 0,
                `error_reason` TEXT DEFAULT NULL,
                `created_at` DATETIME NOT NULL,
                `updated_at` DATETIME NOT NULL,
                PRIMARY KEY (`id`),
                UNIQUE KEY `unique_idempotency_key` (`idempotency_key`),
                KEY `idx_status` (`status`),
                KEY `idx_event_time` (`event_time`),
                KEY `idx_updated_at` (`updated_at`)
            ) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB;
SQL;

        $this->database->statement($sql);
    }
};

