-- Migration 015: job_queue table for async background processing.
-- Enables real email sends, FCM push notifications, OAuth token refresh,
-- and any other deferred work that must not block the HTTP request cycle.

CREATE TABLE IF NOT EXISTS job_queue (
    id         INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    type       VARCHAR(60)  NOT NULL,
    payload_json TEXT        NOT NULL,
    status     ENUM('queued','processing','done','failed') NOT NULL DEFAULT 'queued',
    attempts   TINYINT UNSIGNED NOT NULL DEFAULT 0,
    run_at     DATETIME     NOT NULL DEFAULT CURRENT_TIMESTAMP,
    created_at DATETIME     NOT NULL DEFAULT CURRENT_TIMESTAMP,
    processed_at DATETIME   NULL,
    INDEX idx_jq_status_run (status, run_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
