//! Manual test for regime orchestrator database integration
//! Run with: cargo run --bin test_regime_db_integration

use chrono::Utc;
use ml::regime::orchestrator::{Bar, RegimeOrchestrator};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Database connection
    let database_url = "postgresql://foxhunt:foxhunt_dev_password@localhost:5432/foxhunt";
    let pool = sqlx::PgPool::connect(database_url).await?;

    println!("✓ Connected to database");

    // Create orchestrator
    let mut orchestrator = RegimeOrchestrator::new(pool.clone()).await?;
    println!("✓ Created RegimeOrchestrator");

    // Create test bars (trending pattern)
    let base_time = Utc::now();
    let bars: Vec<Bar> = (0..100)
        .map(|i| {
            let price = 4500.0 + (i as f64 * 2.0); // Strong uptrend
            Bar {
                timestamp: base_time + chrono::Duration::seconds(i as i64 * 60),
                open: price,
                high: price + 1.0,
                low: price - 0.5,
                close: price + 0.8,
                volume: 1000.0,
            }
        })
        .collect();

    println!("✓ Created {} test bars", bars.len());

    // Run detection
    let regime_state = orchestrator
        .detect_and_persist("ES.FUT", &bars)
        .await?;

    println!("✓ Detected regime: {}", regime_state.regime);
    println!("  Confidence: {:.2}", regime_state.confidence);
    println!("  ADX: {:?}", regime_state.adx);

    // Query database
    let regime_count: i64 = sqlx::query_scalar(
        "SELECT COUNT(*) FROM regime_states WHERE symbol = 'ES.FUT'"
    )
    .fetch_one(&pool)
    .await?;

    println!("✓ Database regime_states rows: {}", regime_count);

    let transition_count: i64 = sqlx::query_scalar(
        "SELECT COUNT(*) FROM regime_transitions WHERE symbol = 'ES.FUT'"
    )
    .fetch_one(&pool)
    .await?;

    println!("✓ Database regime_transitions rows: {}", transition_count);

    // Print final status
    println!("\n=== REGIME DETECTION INTEGRATION: PASS ===");
    println!("Test execution: PASS");
    println!("Database population: OK");
    println!("Regime states: {} rows", regime_count);
    println!("Transitions: {} rows", transition_count);

    Ok(())
}
