-- Test Fixture: Regime Detection Tables
-- Minimal schema for orchestrator integration tests
-- Based on migration 045_wave_d_regime_tracking.sql

-- Regime States Table
CREATE TABLE IF NOT EXISTS regime_states (
    id BIGSERIAL PRIMARY KEY,
    symbol TEXT NOT NULL,
    event_timestamp TIMESTAMPTZ NOT NULL,
    regime TEXT NOT NULL CHECK (regime IN ('Normal', 'Trending', 'Ranging', 'Volatile', 'Crisis', 'Illiquid', 'Momentum')),
    confidence DOUBLE PRECISION NOT NULL CHECK (confidence >= 0.0 AND confidence <= 1.0),

    -- CUSUM metrics
    cusum_s_plus DOUBLE PRECISION,
    cusum_s_minus DOUBLE PRECISION,

    -- ADX & Directional Indicators
    adx DOUBLE PRECISION CHECK (adx IS NULL OR (adx >= 0.0 AND adx <= 100.0)),

    -- Regime stability metrics
    stability DOUBLE PRECISION CHECK (stability IS NULL OR (stability >= 0.0 AND stability <= 1.0)),

    created_at TIMESTAMPTZ DEFAULT NOW(),

    CONSTRAINT unique_regime_state UNIQUE (symbol, event_timestamp)
);

CREATE INDEX idx_regime_states_symbol_timestamp ON regime_states(symbol, event_timestamp DESC);

-- Regime Transitions Table
CREATE TABLE IF NOT EXISTS regime_transitions (
    id BIGSERIAL PRIMARY KEY,
    symbol TEXT NOT NULL,
    event_timestamp TIMESTAMPTZ NOT NULL,
    from_regime TEXT NOT NULL CHECK (from_regime IN ('Normal', 'Trending', 'Ranging', 'Volatile', 'Crisis', 'Illiquid', 'Momentum')),
    to_regime TEXT NOT NULL CHECK (to_regime IN ('Normal', 'Trending', 'Ranging', 'Volatile', 'Crisis', 'Illiquid', 'Momentum')),
    duration_bars INTEGER CHECK (duration_bars >= 0),

    -- Transition probability
    transition_probability DOUBLE PRECISION CHECK (transition_probability IS NULL OR (transition_probability >= 0.0 AND transition_probability <= 1.0)),

    -- Transition context
    adx_at_transition DOUBLE PRECISION,
    cusum_alert_triggered BOOLEAN DEFAULT FALSE,

    created_at TIMESTAMPTZ DEFAULT NOW(),

    CONSTRAINT regime_transition_valid CHECK (from_regime != to_regime)
);

CREATE INDEX idx_regime_transitions_symbol_timestamp ON regime_transitions(symbol, event_timestamp DESC);
