-- =============================================================================
-- Job Scheduler Tables — MySQL
-- =============================================================================
-- Auto-executed ketika JOB_ENABLED=true
-- Menggunakan InnoDB engine dengan utf8mb4 charset
-- Index didefinisikan di dalam CREATE TABLE (MySQL tidak punya CREATE INDEX IF NOT EXISTS)
-- Tipe data: VARCHAR, INT, DATETIME, TEXT
-- =============================================================================

-- -----------------------------------------------------------------------------
-- Table: job_registry
-- Menyimpan definisi dan status terkini setiap job yang terdaftar
-- -----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS job_registry (
    job_name            VARCHAR(100)    NOT NULL,
    display_name        VARCHAR(255)    NULL,
    type                VARCHAR(20)     NOT NULL DEFAULT 'cron',
    schedule            VARCHAR(50)     NULL,
    queue               VARCHAR(20)     NOT NULL DEFAULT 'default',
    handler             VARCHAR(255)    NOT NULL,
    config_file         VARCHAR(255)    NOT NULL,
    timeout             INT             NOT NULL DEFAULT 30000,
    retry_max           INT             NOT NULL DEFAULT 3,
    retry_backoff       VARCHAR(20)     NOT NULL DEFAULT 'exponential',
    status              VARCHAR(20)     NOT NULL DEFAULT 'offline',
    last_run_at         DATETIME        NULL,
    last_status         VARCHAR(20)     NULL,
    last_duration_ms    INT             NULL,
    next_run_at         DATETIME        NULL,
    total_success       INT             NOT NULL DEFAULT 0,
    total_failed        INT             NOT NULL DEFAULT 0,
    created_at          DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at          DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (job_name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- -----------------------------------------------------------------------------
-- Table: job_history
-- Menyimpan setiap record eksekusi job beserta hasilnya
-- Index didefinisikan inline karena MySQL tidak mendukung CREATE INDEX IF NOT EXISTS
-- -----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS job_history (
    id                  VARCHAR(70)     NOT NULL,
    job_name            VARCHAR(100)    NOT NULL,
    bullmq_job_id       VARCHAR(100)    NULL,
    trigger_type        VARCHAR(20)     NOT NULL,
    triggered_by        VARCHAR(100)    NOT NULL DEFAULT 'system',
    status              VARCHAR(20)     NOT NULL,
    queue               VARCHAR(20)     NOT NULL DEFAULT 'default',
    attempt             INT             NOT NULL DEFAULT 1,
    config_file         VARCHAR(255)    NULL,
    started_at          DATETIME        NOT NULL,
    completed_at        DATETIME        NULL,
    duration_ms         INT             NULL,
    progress            INT             NULL,
    result_summary      VARCHAR(500)    NULL,
    error_message       VARCHAR(1000)   NULL,
    error_stack         TEXT            NULL,
    worker_pid          INT             NULL,
    created_at          DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    INDEX idx_job_history_name_started (job_name, started_at DESC),
    INDEX idx_job_history_status_started (status, started_at DESC),
    INDEX idx_job_history_started (started_at DESC),
    INDEX idx_job_history_bullmq_id (bullmq_job_id),
    CONSTRAINT fk_job_history_registry
        FOREIGN KEY (job_name) REFERENCES job_registry(job_name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
