-- =============================================================================
-- SageAPI Cache Tables DDL
-- These tables store synchronized data from the Sage database.
-- Run against the sageapi database.
-- =============================================================================

-- Checkpoint table: tracks the last sync timestamp for each module
CREATE TABLE IF NOT EXISTS sync_state (
    state_key       VARCHAR(64)   NOT NULL PRIMARY KEY,
    last_sync_ts    VARCHAR(64)   DEFAULT NULL,
    created_at      VARCHAR(32)   NOT NULL,
    updated_at      VARCHAR(32)   NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- =============================================================================
-- users_cache: synced from Sage users / organi / organization tables
-- =============================================================================
CREATE TABLE IF NOT EXISTS users_cache (
    user_id         BIGINT        NOT NULL PRIMARY KEY,
    username        VARCHAR(128)  DEFAULT NULL,
    email           VARCHAR(256)  DEFAULT NULL,
    user_status     INT           DEFAULT 0,
    user_created_at VARCHAR(32)   DEFAULT NULL,
    user_updated_at VARCHAR(32)   DEFAULT NULL,
    organi_id       BIGINT        DEFAULT NULL,
    organi_name     VARCHAR(256)  DEFAULT NULL,
    organi_parent_id BIGINT       DEFAULT NULL,
    org_id          BIGINT        DEFAULT NULL,
    org_name        VARCHAR(256)  DEFAULT NULL,
    org_type        VARCHAR(64)   DEFAULT NULL,
    org_status      INT           DEFAULT 0,
    org_updated_at  VARCHAR(32)   DEFAULT NULL,
    synced_at       VARCHAR(32)   NOT NULL,
    UNIQUE KEY uk_organi (organi_id),
    KEY idx_updated (user_updated_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- =============================================================================
-- pricing_cache: synced from Sage pricing_program / pricing_program_timing
-- =============================================================================
CREATE TABLE IF NOT EXISTS pricing_cache (
    program_id          BIGINT        NOT NULL,
    program_name        VARCHAR(256)  DEFAULT NULL,
    program_code        VARCHAR(128)  DEFAULT NULL,
    program_type        VARCHAR(64)   DEFAULT NULL,
    program_status      INT           DEFAULT 0,
    description         TEXT          DEFAULT NULL,
    program_created_at  VARCHAR(32)   DEFAULT NULL,
    program_updated_at  VARCHAR(32)   DEFAULT NULL,
    timing_id           BIGINT        DEFAULT NULL,
    start_time          VARCHAR(32)   DEFAULT NULL,
    end_time            VARCHAR(32)   DEFAULT NULL,
    duration            INT           DEFAULT NULL,
    repeat_rule         VARCHAR(256)  DEFAULT NULL,
    timezone            VARCHAR(64)   DEFAULT NULL,
    timing_status       INT           DEFAULT 0,
    timing_updated_at   VARCHAR(32)   DEFAULT NULL,
    synced_at           VARCHAR(32)   NOT NULL,
    PRIMARY KEY (program_id, timing_id),
    KEY idx_program_updated (program_updated_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- =============================================================================
-- uapi_cache: synced from Sage uapi / upapp tables
-- =============================================================================
CREATE TABLE IF NOT EXISTS uapi_cache (
    uapi_id             BIGINT        NOT NULL,
    api_name            VARCHAR(256)  DEFAULT NULL,
    api_path            VARCHAR(512)  DEFAULT NULL,
    api_method          VARCHAR(16)   DEFAULT 'GET',
    api_version         VARCHAR(32)   DEFAULT NULL,
    api_desc            TEXT          DEFAULT NULL,
    uapi_status         INT           DEFAULT 0,
    auth_required       TINYINT       DEFAULT 0,
    uapi_created_at     VARCHAR(32)   DEFAULT NULL,
    uapi_updated_at     VARCHAR(32)   DEFAULT NULL,
    upapp_id            BIGINT        DEFAULT NULL,
    app_name            VARCHAR(256)  DEFAULT NULL,
    app_code            VARCHAR(128)  DEFAULT NULL,
    app_type            VARCHAR(64)   DEFAULT NULL,
    app_desc            TEXT          DEFAULT NULL,
    app_owner           VARCHAR(128)  DEFAULT NULL,
    upapp_status        INT           DEFAULT 0,
    upapp_updated_at    VARCHAR(32)   DEFAULT NULL,
    synced_at           VARCHAR(32)   NOT NULL,
    PRIMARY KEY (uapi_id, upapp_id),
    KEY idx_uapi_updated (uapi_updated_at),
    KEY idx_upapp_id (upapp_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- =============================================================================
-- llmage_cache: synced from Sage llm / llmcatelog / llm_api_map tables
-- =============================================================================
CREATE TABLE IF NOT EXISTS llmage_cache (
    llm_id              BIGINT        NOT NULL,
    model_name          VARCHAR(256)  DEFAULT NULL,
    model_version       VARCHAR(64)   DEFAULT NULL,
    provider            VARCHAR(128)  DEFAULT NULL,
    model_type          VARCHAR(64)   DEFAULT NULL,
    llm_status          INT           DEFAULT 0,
    llm_description     TEXT          DEFAULT NULL,
    llm_created_at      VARCHAR(32)   DEFAULT NULL,
    llm_updated_at      VARCHAR(32)   DEFAULT NULL,
    catelog_id          BIGINT        DEFAULT NULL,
    catelog_name        VARCHAR(256)  DEFAULT NULL,
    catelog_code        VARCHAR(128)  DEFAULT NULL,
    catelog_sort        INT           DEFAULT 0,
    catelog_status      INT           DEFAULT 0,
    catelog_updated_at  VARCHAR(32)   DEFAULT NULL,
    api_map_id          BIGINT        DEFAULT NULL,
    api_name            VARCHAR(256)  DEFAULT NULL,
    api_endpoint        VARCHAR(512)  DEFAULT NULL,
    api_version         VARCHAR(32)   DEFAULT NULL,
    auth_type           VARCHAR(32)   DEFAULT NULL,
    rate_limit          INT           DEFAULT NULL,
    api_map_status      INT           DEFAULT 0,
    api_map_updated_at  VARCHAR(32)   DEFAULT NULL,
    synced_at           VARCHAR(32)   NOT NULL,
    PRIMARY KEY (llm_id, catelog_id, api_map_id),
    KEY idx_llm_updated (llm_updated_at),
    KEY idx_catelog_id (catelog_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
