-- =============================================================================
-- E2E test database schema for the ``sientia_data`` namespace.
--
-- Mirrors the production DDL one-to-one so any change in production can be
-- pasted directly into this file. The conftest fixture loads this SQL into the
-- testcontainers Postgres before each test run.
--
-- Notes on differences from production:
--  * Tables that are partitioned in production (e.g. ``simple_metrics``,
--    ``transformed_data``, ``drift_metrics``) are created as plain tables
--    here because the test suite does not exercise partition pruning.
--  * Indexes are intentionally omitted; tests rely on functional behavior,
--    not query plans.
-- =============================================================================

CREATE SCHEMA IF NOT EXISTS sientia_data;

-- -----------------------------------------------------------------------------
-- sientia_data.laborious_data
-- -----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS sientia_data.laborious_data (
    model_id    int4 NOT NULL,
    variable    text NOT NULL,
    value       numeric NULL,
    "timestamp" timestamptz NOT NULL,
    created_at  timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL,
    CONSTRAINT unique_timestamp_variable
        UNIQUE (model_id, "timestamp", variable)
);

-- -----------------------------------------------------------------------------
-- sientia_data.predictions
-- -----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS sientia_data.predictions (
    model_id              int4 NOT NULL,
    prediction            numeric NULL,
    prediction_confidence numeric NOT NULL,
    response_time         numeric NOT NULL,
    prediction_status     text NOT NULL,
    "timestamp"           timestamptz NOT NULL,
    created_at            timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL,
    "comments"            text NULL,
    CONSTRAINT unique_model_id_timestamp
        UNIQUE (model_id, "timestamp")
);

-- -----------------------------------------------------------------------------
-- sientia_data.transformed_data
--   Production: PARTITION BY RANGE (created_at). Tests use a plain table.
-- -----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS sientia_data.transformed_data (
    id          SERIAL NOT NULL,
    model_id    int4 NOT NULL,
    variable    text NOT NULL,
    value       numeric NULL,
    "timestamp" timestamptz NOT NULL,
    created_at  timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL,
    PRIMARY KEY (id, created_at)
);

-- -----------------------------------------------------------------------------
-- sientia_data.drift_metrics
--   Production: PARTITION BY RANGE (created_at). Tests use a plain table.
-- -----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS sientia_data.drift_metrics (
    id               SERIAL NOT NULL,
    model_id         text NOT NULL,
    feature          text NULL,
    method           text NOT NULL,
    value            numeric NOT NULL,
    alert            bool NOT NULL,
    chunk_index      int4 NOT NULL,
    chunk_start_date text NOT NULL,
    chunk_end_date   text NOT NULL,
    accurate         bool NOT NULL,
    "timestamp"      timestamptz NULL,
    created_at       timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL,
    PRIMARY KEY (id, created_at)
);

-- -----------------------------------------------------------------------------
-- sientia_data.simple_metrics
--   Production: PARTITION BY RANGE (created_at). Tests use a plain table.
-- -----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS sientia_data.simple_metrics (
    id               SERIAL NOT NULL,
    model_id         text NOT NULL,
    metric           text NOT NULL,
    value            numeric NOT NULL,
    "timestamp"      timestamptz NULL,
    data_size        int4 NOT NULL,
    interval_minutes int4 NOT NULL,
    created_at       timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL,
    PRIMARY KEY (id, created_at)
);

-- -----------------------------------------------------------------------------
-- sientia_data.log_retrain
--   No primary key in production; all columns nullable.
-- -----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS sientia_data.log_retrain (
    mlflow_experiment_id int8 NULL,
    mlflow_run_id        text NULL,
    model_id             text NULL,
    model_name           text NULL,
    status               text NULL,
    "timestamp"          timestamptz NULL,
    "version"            text NULL
);
