-- V3__Add_Engine_History_Schema.sql
-- Engine History Tables

CREATE SCHEMA IF NOT EXISTS engine_history;

-- Market Raw History
CREATE TABLE IF NOT EXISTS engine_history.market_raw_history (
    id BIGSERIAL PRIMARY KEY,
    source_id TEXT NOT NULL,
    observed_at TEXT NOT NULL,
    source_name TEXT NOT NULL,
    instrument_id TEXT NOT NULL,
    field_name TEXT NOT NULL,
    field_value TEXT NOT NULL,
    unit TEXT NOT NULL,
    provenance JSONB NOT NULL DEFAULT '{}'::jsonb,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_market_raw_history_created_at ON engine_history.market_raw_history (created_at DESC);

-- Factor Version History
CREATE TABLE IF NOT EXISTS engine_history.factor_version_history (
    id BIGSERIAL PRIMARY KEY,
    factor_id TEXT NOT NULL,
    factor_version TEXT NOT NULL,
    effective_from TEXT NOT NULL,
    effective_to TEXT NOT NULL,
    formula_id TEXT NOT NULL,
    source_version TEXT NOT NULL,
    provenance JSONB NOT NULL DEFAULT '{}'::jsonb,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_factor_version_history_created_at ON engine_history.factor_version_history (created_at DESC);

-- Factor Output History
CREATE TABLE IF NOT EXISTS engine_history.factor_output_history (
    id BIGSERIAL PRIMARY KEY,
    factor_output_id TEXT NOT NULL,
    observed_at TEXT NOT NULL,
    factor_id TEXT NOT NULL,
    factor_version TEXT NOT NULL,
    output_value TEXT NOT NULL,
    output_gate TEXT NOT NULL,
    source_version TEXT NOT NULL,
    provenance JSONB NOT NULL DEFAULT '{}'::jsonb,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_factor_output_history_created_at ON engine_history.factor_output_history (created_at DESC);

-- Decision Result History
CREATE TABLE IF NOT EXISTS engine_history.decision_result_history (
    id BIGSERIAL PRIMARY KEY,
    decision_id TEXT NOT NULL,
    decided_at TEXT NOT NULL,
    instrument_id TEXT NOT NULL,
    action TEXT NOT NULL,
    gate TEXT NOT NULL,
    score TEXT NOT NULL,
    source_version TEXT NOT NULL,
    provenance JSONB NOT NULL DEFAULT '{}'::jsonb,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_decision_result_history_created_at ON engine_history.decision_result_history (created_at DESC);

-- Market vs Engine Gap History
CREATE TABLE IF NOT EXISTS engine_history.market_vs_engine_gap_history (
    id BIGSERIAL PRIMARY KEY,
    gap_id TEXT NOT NULL,
    observed_at TEXT NOT NULL,
    instrument_id TEXT NOT NULL,
    metric_name TEXT NOT NULL,
    market_value TEXT NOT NULL,
    engine_value TEXT NOT NULL,
    gap_value TEXT NOT NULL,
    gap_pct TEXT NOT NULL,
    source_version TEXT NOT NULL,
    provenance JSONB NOT NULL DEFAULT '{}'::jsonb,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_market_vs_engine_gap_history_created_at ON engine_history.market_vs_engine_gap_history (created_at DESC);
