-- trueref schema V1
-- All UUIDs stored as CHAR(36) for H2 portability.

CREATE TABLE repositories (
    id                       CHAR(36)      PRIMARY KEY,
    name                     VARCHAR(512)  NOT NULL UNIQUE,
    remote_url               VARCHAR(2048) NULL,
    local_path               VARCHAR(2048) NOT NULL,
    managed_clone            BOOLEAN       NOT NULL,
    ignore_globs             CLOB          NOT NULL,        -- JSON array of strings
    max_file_size_bytes      BIGINT        NOT NULL,
    poll_interval_seconds    BIGINT        NOT NULL,
    tag_cap                  INT           NOT NULL,
    version_mapping_rules    CLOB          NOT NULL,        -- JSON array of TagPattern
    created_at               TIMESTAMP(9) WITH TIME ZONE NOT NULL,
    updated_at               TIMESTAMP(9) WITH TIME ZONE NOT NULL
);

CREATE TABLE versions (
    id              CHAR(36)      PRIMARY KEY,
    repo_id         CHAR(36)      NOT NULL REFERENCES repositories(id) ON DELETE CASCADE,
    tag             VARCHAR(512)  NOT NULL,
    commit_sha      CHAR(40)      NOT NULL,
    status          VARCHAR(32)   NOT NULL,
    indexed_at      TIMESTAMP(9) WITH TIME ZONE NULL,
    chunk_count     INT           NOT NULL DEFAULT 0,
    error_message   CLOB          NULL,
    UNIQUE (repo_id, tag)
);
CREATE INDEX idx_versions_repo_status ON versions(repo_id, status);

CREATE TABLE ingestion_jobs (
    id           CHAR(36)     PRIMARY KEY,
    repo_id      CHAR(36)     NOT NULL REFERENCES repositories(id) ON DELETE CASCADE,
    version_id   CHAR(36)     NULL     REFERENCES versions(id) ON DELETE CASCADE,
    type         VARCHAR(32)  NOT NULL,
    status       VARCHAR(32)  NOT NULL,
    started_at   TIMESTAMP(9) WITH TIME ZONE NULL,
    finished_at  TIMESTAMP(9) WITH TIME ZONE NULL,
    created_at   TIMESTAMP(9) WITH TIME ZONE NOT NULL
);
CREATE INDEX idx_jobs_repo_status ON ingestion_jobs(repo_id, status);
CREATE INDEX idx_jobs_status_created ON ingestion_jobs(status, created_at);

CREATE TABLE job_stages (
    job_id           CHAR(36)     NOT NULL REFERENCES ingestion_jobs(id) ON DELETE CASCADE,
    name             VARCHAR(32)  NOT NULL,
    status           VARCHAR(32)  NOT NULL,
    started_at       TIMESTAMP(9) WITH TIME ZONE NULL,
    finished_at      TIMESTAMP(9) WITH TIME ZONE NULL,
    items_processed  BIGINT       NOT NULL DEFAULT 0,
    items_total      BIGINT       NOT NULL DEFAULT 0,
    bytes_processed  BIGINT       NOT NULL DEFAULT 0,
    error_message    CLOB         NULL,
    PRIMARY KEY (job_id, name)
);

-- Persisted log buffer (last N per job kept by application logic; SSE streams from in-memory bus).
CREATE TABLE job_log_events (
    id        BIGINT       GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    job_id    CHAR(36)     NOT NULL REFERENCES ingestion_jobs(id) ON DELETE CASCADE,
    ts        TIMESTAMP(9) WITH TIME ZONE NOT NULL,
    level     VARCHAR(8)   NOT NULL,
    stage     VARCHAR(32),
    message   CLOB         NOT NULL
);
CREATE INDEX idx_job_log_job_ts ON job_log_events(job_id, ts);
