-- ================================================================================================
-- Validation Script: Wave D Regime Detection Database Persistence
-- Agent IMPL-24: Integration Test Database Validation
-- ================================================================================================
-- 
-- PURPOSE: Validate that regime_states, regime_transitions, and adaptive_strategy_metrics
-- are properly populated and contain valid data.
--
-- USAGE:
--   psql postgresql://foxhunt:foxhunt_dev_password@localhost:5432/foxhunt -f validate_regime_data.sql
--
-- EXPECTED OUTPUT:
--   - All checks should return "PASS"
--   - No errors or warnings
--   - Data counts > 0 for populated tables
-- ================================================================================================

\echo '========================================================================'
\echo 'Wave D Regime Detection Database Validation'
\echo 'Agent IMPL-24: Database Persistence Verification'
\echo '========================================================================'
\echo ''

-- ================================================================================================
-- CHECK 1: Regime Coverage by Symbol
-- Verifies that regime states exist for primary symbols
-- ================================================================================================
\echo '✓ CHECK 1: Regime Coverage by Symbol'
SELECT 
    symbol,
    COUNT(*) as regime_state_count,
    COUNT(DISTINCT regime) as unique_regimes,
    CASE 
        WHEN COUNT(*) > 0 THEN '✓ PASS'
        ELSE '✗ FAIL'
    END as status
FROM regime_states 
GROUP BY symbol
ORDER BY symbol;

\echo ''

-- ================================================================================================
-- CHECK 2: Regime State Data Quality
-- Verifies confidence scores, ADX values, and CUSUM metrics are within valid ranges
-- ================================================================================================
\echo '✓ CHECK 2: Regime State Data Quality'
SELECT
    COUNT(*) as total_states,
    COUNT(*) FILTER (WHERE confidence >= 0.0 AND confidence <= 1.0) as valid_confidence_count,
    COUNT(*) FILTER (WHERE adx IS NULL OR (adx >= 0.0 AND adx <= 100.0)) as valid_adx_count,
    COUNT(*) FILTER (WHERE stability IS NULL OR (stability >= 0.0 AND stability <= 1.0)) as valid_stability_count,
    COUNT(*) FILTER (WHERE cusum_s_plus IS NOT NULL) as has_cusum_plus,
    COUNT(*) FILTER (WHERE cusum_s_minus IS NOT NULL) as has_cusum_minus,
    CASE
        WHEN COUNT(*) = COUNT(*) FILTER (WHERE confidence >= 0.0 AND confidence <= 1.0)
             AND COUNT(*) = COUNT(*) FILTER (WHERE adx IS NULL OR (adx >= 0.0 AND adx <= 100.0))
             AND COUNT(*) = COUNT(*) FILTER (WHERE stability IS NULL OR (stability >= 0.0 AND stability <= 1.0))
        THEN '✓ PASS'
        ELSE '✗ FAIL'
    END as status
FROM regime_states;

\echo ''

-- ================================================================================================
-- CHECK 3: Transition Matrix Completeness
-- Verifies regime transitions are tracked and form a valid transition matrix
-- ================================================================================================
\echo '✓ CHECK 3: Transition Matrix Completeness'
SELECT
    from_regime,
    to_regime,
    COUNT(*) as transition_count,
    AVG(duration_bars) as avg_duration_bars,
    CASE
        WHEN COUNT(*) > 0 AND from_regime != to_regime THEN '✓ PASS'
        ELSE '✗ FAIL'
    END as status
FROM regime_transitions
GROUP BY from_regime, to_regime
ORDER BY from_regime, to_regime;

\echo ''

-- ================================================================================================
-- CHECK 4: Adaptive Strategy Metrics Validity
-- Verifies position/stop-loss multipliers are within operational ranges
-- ================================================================================================
\echo '✓ CHECK 4: Adaptive Strategy Metrics Validity'
SELECT
    regime,
    COUNT(*) as metrics_count,
    MIN(position_multiplier) as min_pos_mult,
    MAX(position_multiplier) as max_pos_mult,
    MIN(stop_loss_multiplier) as min_stop_mult,
    MAX(stop_loss_multiplier) as max_stop_mult,
    CASE
        WHEN MIN(position_multiplier) >= 0.0 AND MAX(position_multiplier) <= 2.0
             AND MIN(stop_loss_multiplier) >= 1.0 AND MAX(stop_loss_multiplier) <= 5.0
        THEN '✓ PASS'
        ELSE '✗ FAIL'
    END as status
FROM adaptive_strategy_metrics
GROUP BY regime
ORDER BY regime;

\echo ''

-- ================================================================================================
-- CHECK 5: Timestamp Recency
-- Verifies that regime states have been updated recently (within last 24 hours)
-- ================================================================================================
\echo '✓ CHECK 5: Timestamp Recency'
SELECT
    symbol,
    MAX(event_timestamp) as latest_timestamp,
    EXTRACT(EPOCH FROM (NOW() - MAX(event_timestamp))) / 3600 as hours_since_update,
    CASE
        WHEN MAX(event_timestamp) >= NOW() - INTERVAL '24 hours' THEN '✓ PASS'
        WHEN MAX(event_timestamp) >= NOW() - INTERVAL '7 days' THEN '⚠ WARNING (data is old)'
        ELSE '✗ FAIL (data is stale)'
    END as status
FROM regime_states
GROUP BY symbol
ORDER BY symbol;

\echo ''

-- ================================================================================================
-- CHECK 6: Grafana Dashboard Query Compatibility
-- Verifies that queries used by Grafana dashboards return valid data
-- ================================================================================================
\echo '✓ CHECK 6: Grafana Dashboard Query Compatibility'
\echo 'Testing regime distribution query...'
SELECT
    symbol,
    regime,
    COUNT(*) as count,
    AVG(confidence) as avg_confidence
FROM regime_states
WHERE event_timestamp >= NOW() - INTERVAL '1 hour'
GROUP BY symbol, regime
ORDER BY symbol, regime
LIMIT 10;

\echo ''
\echo 'Testing time-series query...'
SELECT
    symbol,
    event_timestamp,
    regime,
    confidence,
    adx
FROM regime_states
WHERE symbol IN ('ES.FUT', 'NQ.FUT')
ORDER BY event_timestamp DESC
LIMIT 10;

\echo ''

-- ================================================================================================
-- CHECK 7: Latest Regime State Function
-- Verifies get_latest_regime() function works correctly
-- ================================================================================================
\echo '✓ CHECK 7: Latest Regime State Function'
SELECT
    'ES.FUT' as symbol,
    regime,
    confidence,
    event_timestamp,
    cusum_s_plus,
    adx,
    stability,
    CASE
        WHEN regime IS NOT NULL AND confidence >= 0.0 THEN '✓ PASS'
        ELSE '✗ FAIL'
    END as status
FROM get_latest_regime('ES.FUT')
LIMIT 1;

\echo ''

-- ================================================================================================
-- CHECK 8: Regime Transition Matrix Function
-- Verifies get_regime_transition_matrix() function calculates probabilities correctly
-- ================================================================================================
\echo '✓ CHECK 8: Regime Transition Matrix Function'
SELECT
    from_regime,
    to_regime,
    transition_count,
    transition_probability,
    CASE
        WHEN transition_probability >= 0.0 AND transition_probability <= 1.0 THEN '✓ PASS'
        ELSE '✗ FAIL'
    END as status
FROM get_regime_transition_matrix('ES.FUT', 168)
LIMIT 10;

\echo ''

-- ================================================================================================
-- CHECK 9: Regime Performance Function
-- Verifies get_regime_performance() function returns valid metrics
-- ================================================================================================
\echo '✓ CHECK 9: Regime Performance Function'
SELECT
    regime,
    total_trades,
    win_rate,
    avg_sharpe,
    avg_position_multiplier,
    avg_stop_loss_multiplier,
    total_pnl,
    avg_risk_utilization,
    CASE
        WHEN win_rate IS NULL OR (win_rate >= 0.0 AND win_rate <= 1.0) THEN '✓ PASS'
        ELSE '✗ FAIL'
    END as status
FROM get_regime_performance('ES.FUT', 24)
LIMIT 10;

\echo ''

-- ================================================================================================
-- CHECK 10: Index Performance Validation
-- Verifies that database indices exist and are being used effectively
-- ================================================================================================
\echo '✓ CHECK 10: Index Performance Validation'
SELECT
    schemaname,
    tablename,
    indexname,
    CASE
        WHEN indexname IS NOT NULL THEN '✓ PASS'
        ELSE '✗ FAIL'
    END as status
FROM pg_indexes
WHERE tablename IN ('regime_states', 'regime_transitions', 'adaptive_strategy_metrics')
ORDER BY tablename, indexname;

\echo ''

-- ================================================================================================
-- SUMMARY: Overall Data Health
-- Provides a high-level summary of regime detection data health
-- ================================================================================================
\echo '========================================================================'
\echo 'SUMMARY: Overall Data Health'
\echo '========================================================================'
SELECT
    'regime_states' as table_name,
    COUNT(*) as total_rows,
    COUNT(DISTINCT symbol) as unique_symbols,
    COUNT(DISTINCT regime) as unique_regimes,
    MIN(event_timestamp) as earliest_timestamp,
    MAX(event_timestamp) as latest_timestamp
FROM regime_states
UNION ALL
SELECT
    'regime_transitions' as table_name,
    COUNT(*) as total_rows,
    COUNT(DISTINCT symbol) as unique_symbols,
    COUNT(DISTINCT from_regime || '->' || to_regime) as unique_transitions,
    MIN(event_timestamp) as earliest_timestamp,
    MAX(event_timestamp) as latest_timestamp
FROM regime_transitions
UNION ALL
SELECT
    'adaptive_strategy_metrics' as table_name,
    COUNT(*) as total_rows,
    COUNT(DISTINCT symbol) as unique_symbols,
    COUNT(DISTINCT regime) as unique_regimes,
    MIN(event_timestamp) as earliest_timestamp,
    MAX(event_timestamp) as latest_timestamp
FROM adaptive_strategy_metrics;

\echo ''
\echo '========================================================================'
\echo 'Validation Complete!'
\echo '========================================================================'
\echo 'All checks should show "✓ PASS" for production readiness.'
\echo 'If any checks show "✗ FAIL", investigate data quality issues.'
\echo ''
