<?php

declare(strict_types=1);
if ( ! defined( 'ABSPATH' ) ) { exit; }

/**
 * Migration 0002 — Task Queue
 *
 * Creates the instinctor_jobs table used by the Queue facade (T6).
 * Status: pending | running | completed | failed
 * available_at — delay support; reserved_at — prevents double-pickup
 */
return [
    'description' => 'Task queue — instinctor_jobs table',

    'up' => [
        "CREATE TABLE IF NOT EXISTS {prefix}instinctor_jobs (
            id           BIGINT UNSIGNED  NOT NULL AUTO_INCREMENT,
            type         VARCHAR(100)     NOT NULL,
            payload      LONGTEXT         NOT NULL,
            status       VARCHAR(20)      NOT NULL DEFAULT 'pending',
            attempts     TINYINT UNSIGNED NOT NULL DEFAULT 0,
            run_at       DATETIME         NOT NULL DEFAULT CURRENT_TIMESTAMP,
            available_at DATETIME         NOT NULL DEFAULT CURRENT_TIMESTAMP,
            reserved_at  DATETIME         NULL,
            error        TEXT             NULL,
            created_at   DATETIME         NOT NULL DEFAULT CURRENT_TIMESTAMP,
            PRIMARY KEY (id),
            KEY idx_queue_pick   (status, available_at, reserved_at),
            KEY idx_queue_type   (type),
            KEY idx_queue_run_at (run_at)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci",
    ],

    'down' => [
        "DROP TABLE IF EXISTS {prefix}instinctor_jobs",
    ],
];
