-- ================================================================================================
-- Test Fixtures: Regime Detection Data for Integration Tests
-- ================================================================================================
-- 
-- Purpose: Provide test data for Kelly Criterion + Regime Detection integration tests
-- Usage: Loaded automatically by integration_kelly_regime.rs tests
--
-- Test Scenarios:
-- 1. Trending regime (1.5x position multiplier, 2.5x stop-loss)
-- 2. Crisis regime (0.2x position multiplier, 4.0x stop-loss)
-- 3. Normal regime (1.0x position multiplier, 2.0x stop-loss)
-- 4. Volatile regime (0.5x position multiplier, 3.0x stop-loss)
-- 5. Ranging regime (0.8x position multiplier, 1.5x stop-loss)
-- ================================================================================================

-- Clean up existing test data
DELETE FROM regime_states WHERE symbol LIKE 'TEST_%' OR symbol IN ('ES.FUT', 'NQ.FUT', 'ZN.FUT', '6E.FUT', 'CL.FUT');
DELETE FROM regime_transitions WHERE symbol LIKE 'TEST_%' OR symbol IN ('ES.FUT', 'NQ.FUT', 'ZN.FUT', '6E.FUT', 'CL.FUT');
DELETE FROM adaptive_strategy_metrics WHERE symbol LIKE 'TEST_%' OR symbol IN ('ES.FUT', 'NQ.FUT', 'ZN.FUT', '6E.FUT', 'CL.FUT');

-- ================================================================================================
-- Test Scenario 1: Trending Regime (ES.FUT)
-- ================================================================================================
INSERT INTO regime_states (
    symbol, event_timestamp, regime, confidence,
    cusum_s_plus, cusum_s_minus, cusum_alert_count,
    adx, plus_di, minus_di,
    stability, entropy
) VALUES (
    'ES.FUT',
    NOW() - INTERVAL '1 hour',
    'Trending',
    0.85,
    3.5,    -- Strong positive CUSUM
    -0.2,   -- Weak negative CUSUM
    2,      -- 2 alerts detected
    42.0,   -- Strong trend (ADX > 40)
    35.0,   -- Positive direction dominant
    18.0,   -- Weaker negative direction
    0.92,   -- High stability (regime is stable)
    0.12    -- Low entropy (regime is clear)
);

-- ================================================================================================
-- Test Scenario 2: Crisis Regime (NQ.FUT)
-- ================================================================================================
INSERT INTO regime_states (
    symbol, event_timestamp, regime, confidence,
    cusum_s_plus, cusum_s_minus, cusum_alert_count,
    adx, plus_di, minus_di,
    stability, entropy
) VALUES (
    'NQ.FUT',
    NOW() - INTERVAL '30 minutes',
    'Crisis',
    0.92,
    -1.2,   -- Negative CUSUM
    -8.5,   -- Very strong negative CUSUM (crisis signal)
    12,     -- Many alerts (high instability)
    55.0,   -- Very strong trend (down)
    10.0,   -- Very weak positive direction
    52.0,   -- Very strong negative direction
    0.65,   -- Lower stability (crisis is volatile)
    0.45    -- Higher entropy (crisis is uncertain)
);

-- ================================================================================================
-- Test Scenario 3: Normal Regime (ZN.FUT)
-- ================================================================================================
INSERT INTO regime_states (
    symbol, event_timestamp, regime, confidence,
    cusum_s_plus, cusum_s_minus, cusum_alert_count,
    adx, plus_di, minus_di,
    stability, entropy
) VALUES (
    'ZN.FUT',
    NOW() - INTERVAL '2 hours',
    'Normal',
    0.88,
    0.5,    -- Mild positive CUSUM
    -0.3,   -- Mild negative CUSUM
    1,      -- Few alerts
    22.0,   -- Moderate ADX (weak trend)
    25.0,   -- Balanced positive direction
    23.0,   -- Balanced negative direction
    0.95,   -- Very high stability (normal market)
    0.08    -- Very low entropy (regime is clear)
);

-- ================================================================================================
-- Test Scenario 4: Volatile Regime (6E.FUT)
-- ================================================================================================
INSERT INTO regime_states (
    symbol, event_timestamp, regime, confidence,
    cusum_s_plus, cusum_s_minus, cusum_alert_count,
    adx, plus_di, minus_di,
    stability, entropy
) VALUES (
    '6E.FUT',
    NOW() - INTERVAL '45 minutes',
    'Volatile',
    0.78,
    1.8,    -- Moderate positive CUSUM
    -1.5,   -- Moderate negative CUSUM
    7,      -- Several alerts
    48.0,   -- Strong ADX (strong volatility)
    32.0,   -- Strong positive swings
    30.0,   -- Strong negative swings
    0.72,   -- Lower stability (volatile market)
    0.35    -- Higher entropy (regime is less clear)
);

-- ================================================================================================
-- Test Scenario 5: Ranging Regime (CL.FUT)
-- ================================================================================================
INSERT INTO regime_states (
    symbol, event_timestamp, regime, confidence,
    cusum_s_plus, cusum_s_minus, cusum_alert_count,
    adx, plus_di, minus_di,
    stability, entropy
) VALUES (
    'CL.FUT',
    NOW() - INTERVAL '90 minutes',
    'Ranging',
    0.82,
    0.2,    -- Very weak positive CUSUM
    -0.1,   -- Very weak negative CUSUM
    0,      -- No alerts (stable range)
    15.0,   -- Low ADX (no trend, ranging)
    20.0,   -- Weak positive direction
    18.0,   -- Weak negative direction
    0.88,   -- High stability (range is stable)
    0.18    -- Low entropy (regime is clear)
);

-- ================================================================================================
-- Regime Transitions (for pattern analysis)
-- ================================================================================================

-- ES.FUT: Normal → Trending transition
INSERT INTO regime_transitions (
    symbol, event_timestamp, from_regime, to_regime,
    duration_bars, transition_probability,
    adx_at_transition, cusum_alert_triggered
) VALUES (
    'ES.FUT',
    NOW() - INTERVAL '1 hour',
    'Normal',
    'Trending',
    150,    -- 150 bars in Normal regime
    0.35,   -- 35% probability of this transition
    38.0,   -- ADX at transition point
    true    -- CUSUM alert triggered the transition
);

-- NQ.FUT: Volatile → Crisis transition
INSERT INTO regime_transitions (
    symbol, event_timestamp, from_regime, to_regime,
    duration_bars, transition_probability,
    adx_at_transition, cusum_alert_triggered
) VALUES (
    'NQ.FUT',
    NOW() - INTERVAL '30 minutes',
    'Volatile',
    'Crisis',
    45,     -- 45 bars in Volatile regime before crisis
    0.12,   -- 12% probability (rare transition)
    50.0,   -- High ADX at crisis point
    true    -- CUSUM alert triggered the transition
);

-- ZN.FUT: Trending → Normal transition
INSERT INTO regime_transitions (
    symbol, event_timestamp, from_regime, to_regime,
    duration_bars, transition_probability,
    adx_at_transition, cusum_alert_triggered
) VALUES (
    'ZN.FUT',
    NOW() - INTERVAL '2 hours',
    'Trending',
    'Normal',
    200,    -- 200 bars in Trending regime
    0.28,   -- 28% probability
    20.0,   -- ADX decreased to 20
    false   -- Gradual transition (no alert)
);

-- ================================================================================================
-- Adaptive Strategy Metrics (for performance tracking)
-- ================================================================================================

-- ES.FUT Trending performance
INSERT INTO adaptive_strategy_metrics (
    symbol, event_timestamp, regime,
    position_multiplier, stop_loss_multiplier,
    regime_sharpe, risk_budget_utilization,
    total_trades, winning_trades, total_pnl
) VALUES (
    'ES.FUT',
    NOW() - INTERVAL '1 hour',
    'Trending',
    1.5,    -- 1.5x position size
    2.5,    -- 2.5x ATR stop-loss
    2.1,    -- Strong Sharpe ratio in trending regime
    0.65,   -- 65% risk budget utilization
    25,     -- 25 trades in this regime
    18,     -- 18 winning trades (72% win rate)
    125000  -- $125k profit
);

-- NQ.FUT Crisis performance
INSERT INTO adaptive_strategy_metrics (
    symbol, event_timestamp, regime,
    position_multiplier, stop_loss_multiplier,
    regime_sharpe, risk_budget_utilization,
    total_trades, winning_trades, total_pnl
) VALUES (
    'NQ.FUT',
    NOW() - INTERVAL '30 minutes',
    'Crisis',
    0.2,    -- 0.2x position size (very conservative)
    4.0,    -- 4.0x ATR stop-loss (very wide)
    0.5,    -- Low Sharpe ratio in crisis
    0.15,   -- 15% risk budget utilization (very low exposure)
    8,      -- Only 8 trades (reduced activity)
    4,      -- 4 winning trades (50% win rate)
    -15000  -- $15k loss (crisis damage control)
);

-- ZN.FUT Normal performance
INSERT INTO adaptive_strategy_metrics (
    symbol, event_timestamp, regime,
    position_multiplier, stop_loss_multiplier,
    regime_sharpe, risk_budget_utilization,
    total_trades, winning_trades, total_pnl
) VALUES (
    'ZN.FUT',
    NOW() - INTERVAL '2 hours',
    'Normal',
    1.0,    -- 1.0x position size (baseline)
    2.0,    -- 2.0x ATR stop-loss (standard)
    1.2,    -- Good Sharpe ratio in normal regime
    0.50,   -- 50% risk budget utilization
    30,     -- 30 trades
    19,     -- 19 winning trades (63% win rate)
    45000   -- $45k profit
);

-- ================================================================================================
-- Test Assertions
-- ================================================================================================

-- Verify regime states inserted correctly
SELECT 
    symbol,
    regime,
    confidence,
    adx,
    stability
FROM regime_states
WHERE symbol IN ('ES.FUT', 'NQ.FUT', 'ZN.FUT', '6E.FUT', 'CL.FUT')
ORDER BY symbol;

-- Verify regime transitions
SELECT 
    symbol,
    from_regime,
    to_regime,
    duration_bars,
    transition_probability
FROM regime_transitions
WHERE symbol IN ('ES.FUT', 'NQ.FUT', 'ZN.FUT')
ORDER BY symbol;

-- Verify adaptive strategy metrics
SELECT 
    symbol,
    regime,
    position_multiplier,
    stop_loss_multiplier,
    regime_sharpe,
    total_trades,
    winning_trades,
    total_pnl
FROM adaptive_strategy_metrics
WHERE symbol IN ('ES.FUT', 'NQ.FUT', 'ZN.FUT')
ORDER BY symbol;

-- ================================================================================================
-- END TEST FIXTURES
-- ================================================================================================
