<?php

/**
 * Migration: script timers table
 * Date: 2026-06-08
 */

return function(\Fury\Modules\ThinBuilder\ThinBuilder $tb) {

    $tb->query("
        CREATE TABLE IF NOT EXISTS shserv_timers (
            id INT AUTO_INCREMENT PRIMARY KEY,
            timer_alias VARCHAR(100) NOT NULL,
            scope_name VARCHAR(100) NOT NULL,
            target_type ENUM('action', 'event', 'regular') NOT NULL,
            target_alias VARCHAR(100) NOT NULL,
            params JSON,
            execute_at DATETIME NOT NULL,
            status ENUM('pending', 'executed', 'cancelled', 'failed') NOT NULL DEFAULT 'pending',
            error_message TEXT,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            executed_at TIMESTAMP NULL,
            UNIQUE KEY uk_alias_scope (timer_alias, scope_name),
            INDEX idx_status_execute (status, execute_at),
            INDEX idx_scope (scope_name)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
    ");

    return true;
};
