-- =============================================================================
-- Job Scheduler Tables — PostgreSQL
-- =============================================================================
-- Auto-executed ketika JOB_ENABLED=true
-- Tabel dibuat di schema default (public) tanpa prefix schema eksplisit
-- Menggunakan TIMESTAMP WITH TIME ZONE (TIMESTAMPTZ) untuk konsistensi timezone
-- =============================================================================

-- -----------------------------------------------------------------------------
-- 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),
    type                VARCHAR(20)     NOT NULL DEFAULT 'cron',
    schedule            VARCHAR(50),
    queue               VARCHAR(20)     NOT NULL DEFAULT 'default',
    handler             VARCHAR(255)    NOT NULL,
    config_file         VARCHAR(255)    NOT NULL,
    timeout             INTEGER         NOT NULL DEFAULT 30000,
    retry_max           INTEGER         NOT NULL DEFAULT 3,
    retry_backoff       VARCHAR(20)     NOT NULL DEFAULT 'exponential',
    status              VARCHAR(20)     NOT NULL DEFAULT 'offline',
    last_run_at         TIMESTAMPTZ,
    last_status         VARCHAR(20),
    last_duration_ms    INTEGER,
    next_run_at         TIMESTAMPTZ,
    total_success       INTEGER         NOT NULL DEFAULT 0,
    total_failed        INTEGER         NOT NULL DEFAULT 0,
    created_at          TIMESTAMPTZ     NOT NULL DEFAULT NOW(),
    updated_at          TIMESTAMPTZ     NOT NULL DEFAULT NOW(),
    CONSTRAINT pk_job_registry PRIMARY KEY (job_name)
);

-- -----------------------------------------------------------------------------
-- Table: job_history
-- Menyimpan setiap record eksekusi job beserta hasilnya
-- -----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS job_history (
    id                  VARCHAR(70)     NOT NULL,
    job_name            VARCHAR(100)    NOT NULL,
    bullmq_job_id       VARCHAR(100),
    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             INTEGER         NOT NULL DEFAULT 1,
    config_file         VARCHAR(255),
    started_at          TIMESTAMPTZ     NOT NULL,
    completed_at        TIMESTAMPTZ,
    duration_ms         INTEGER,
    progress            INTEGER,
    result_summary      VARCHAR(500),
    error_message       VARCHAR(1000),
    error_stack         TEXT,
    worker_pid          INTEGER,
    created_at          TIMESTAMPTZ     NOT NULL DEFAULT NOW(),
    CONSTRAINT pk_job_history PRIMARY KEY (id),
    CONSTRAINT fk_job_history_registry
        FOREIGN KEY (job_name) REFERENCES job_registry(job_name)
);

-- -----------------------------------------------------------------------------
-- Indexes untuk job_history
-- -----------------------------------------------------------------------------
CREATE INDEX IF NOT EXISTS idx_job_history_name_started
    ON job_history (job_name, started_at DESC);

CREATE INDEX IF NOT EXISTS idx_job_history_status_started
    ON job_history (status, started_at DESC);

CREATE INDEX IF NOT EXISTS idx_job_history_started
    ON job_history (started_at DESC);

CREATE INDEX IF NOT EXISTS idx_job_history_bullmq_id
    ON job_history (bullmq_job_id);
