#![allow(unexpected_cfgs)]
#![cfg(feature = "__trading_service_integration")]
//! Integration tests for Asset Selection Module
//!
//! Tests the asset selection logic with real database, ML integration,
//! and fallback behavior when ML is unavailable.

use anyhow::Result;
use chrono::{DateTime, Utc};
use common::ml_strategy::{
    MLModelAdapter, MLPrediction, ProductionFeatureExtractor225, SharedMLStrategy,
};
use sqlx::PgPool;
use std::sync::Arc;
use trading_service::assets::{AssetScore, AssetSelector, ScoringWeights};

/// Mock extractor for test SharedMLStrategy construction
struct TestMockExtractor;

impl ProductionFeatureExtractor225 for TestMockExtractor {
    fn update(&mut self, _price: f64, _volume: f64, _timestamp: DateTime<Utc>) -> Result<()> {
        Ok(())
    }
    fn extract_features(&mut self) -> Result<Vec<f64>> {
        Ok(vec![0.1; 225])
    }
}

/// Helper to create SharedMLStrategy for tests
fn make_test_strategy() -> SharedMLStrategy {
    SharedMLStrategy::new(Box::new(TestMockExtractor), vec![], 0.6)
}

/// Helper to create test database pool
async fn setup_test_db() -> Result<PgPool> {
    let database_url = std::env::var("DATABASE_URL").unwrap_or_else(|_| {
        "postgresql://foxhunt:foxhunt_dev_password@localhost:5432/foxhunt".to_string()
    });

    let pool = PgPool::connect(&database_url).await?;

    // Run migrations if needed
    sqlx::migrate!("../../migrations").run(&pool).await.ok(); // Ignore if already applied

    Ok(pool)
}

/// Helper to seed test universe data
async fn seed_test_universe(pool: &PgPool, universe_id: &str) -> Result<()> {
    // Insert test instruments into universe (JSONB format)
    let symbols = vec!["BTC", "ETH", "SOL", "AVAX", "MATIC"];

    let instruments_json = serde_json::json!(symbols
        .iter()
        .map(|s| {
            serde_json::json!({
                "symbol": s,
                "weight": 0.2,
                "enabled": true
            })
        })
        .collect::<Vec<_>>());

    let criteria_json = serde_json::json!({
        "max_assets": symbols.len(),
        "min_liquidity": 0.3
    });

    let metrics_json = serde_json::json!({
        "total_instruments": symbols.len(),
        "avg_weight": 0.2
    });

    // Insert or update trading universe
    sqlx::query!(
        r#"
        INSERT INTO trading_universes (universe_id, criteria, instruments, metrics)
        VALUES ($1, $2, $3, $4)
        ON CONFLICT (universe_id) DO UPDATE
        SET instruments = $3, metrics = $4, updated_at = NOW()
        "#,
        universe_id,
        criteria_json,
        instruments_json,
        metrics_json
    )
    .execute(pool)
    .await?;

    // Insert mock market data for each symbol
    for symbol in symbols {
        sqlx::query!(
            r#"
            INSERT INTO market_data (symbol, timestamp, timeframe, open_price, high_price, low_price, close_price, volume)
            VALUES ($1, NOW(), '1d', $2, $3, $4, $5, $6)
            ON CONFLICT (symbol, timestamp, timeframe) DO NOTHING
            "#,
            symbol,
            (100.0 + rand::random::<f64>() * 10.0).to_string(),
            (110.0 + rand::random::<f64>() * 10.0).to_string(),
            (90.0 + rand::random::<f64>() * 10.0).to_string(),
            (100.0 + rand::random::<f64>() * 50.0).to_string(),
            (10000.0 + rand::random::<f64>() * 5000.0).to_string()
        )
        .execute(pool)
        .await?;
    }

    Ok(())
}

/// Helper to clean up test data
async fn cleanup_test_data(pool: &PgPool, universe_id: &str) -> Result<()> {
    sqlx::query!(
        "DELETE FROM asset_selections WHERE universe_id = $1",
        universe_id
    )
    .execute(pool)
    .await?;

    sqlx::query!(
        "DELETE FROM trading_universes WHERE universe_id = $1",
        universe_id
    )
    .execute(pool)
    .await?;

    Ok(())
}

#[tokio::test]
async fn test_asset_selector_creation() -> Result<()> {
    let pool = setup_test_db().await?;
    let ml_strategy = Arc::new(make_test_strategy());

    let selector = AssetSelector::new(pool, ml_strategy, None)?;

    // Should use default weights
    assert!(selector.weights.ml_weight == 0.4);

    Ok(())
}

#[tokio::test]
async fn test_asset_selector_custom_weights() -> Result<()> {
    let pool = setup_test_db().await?;
    let ml_strategy = Arc::new(make_test_strategy());

    let mut custom_weights = ScoringWeights {
        ml_weight: 0.5,
        momentum_weight: 0.25,
        value_weight: 0.15,
        liquidity_weight: 0.1,
    };
    custom_weights.normalize();

    let selector = AssetSelector::new(pool, ml_strategy, Some(custom_weights.clone()))?;

    // Should use custom weights
    assert!((selector.weights.ml_weight - custom_weights.ml_weight).abs() < 0.001);

    Ok(())
}

#[tokio::test]
async fn test_select_assets_empty_universe() -> Result<()> {
    let pool = setup_test_db().await?;
    let ml_strategy = Arc::new(make_test_strategy());
    let selector = AssetSelector::new(pool, ml_strategy, None)?;

    let universe_id = "test_empty_universe";
    cleanup_test_data(&selector.pool, universe_id).await?;

    let assets = selector.select_assets(universe_id, 10).await?;

    // Should return empty list for empty universe
    assert_eq!(assets.len(), 0);

    Ok(())
}

#[tokio::test]
async fn test_select_assets_with_universe() -> Result<()> {
    let pool = setup_test_db().await?;
    let universe_id = "test_universe_1";

    // Seed test data
    seed_test_universe(&pool, universe_id).await?;

    let ml_strategy = Arc::new(make_test_strategy());
    let selector = AssetSelector::new(pool, ml_strategy, None)?;

    let assets = selector.select_assets(universe_id, 3).await?;

    // Should return up to 3 assets
    assert!(assets.len() <= 3);

    // All scores should be in valid range [0, 1]
    for asset in &assets {
        assert!(asset.ml_score >= 0.0 && asset.ml_score <= 1.0);
        assert!(asset.momentum_score >= 0.0 && asset.momentum_score <= 1.0);
        assert!(asset.value_score >= 0.0 && asset.value_score <= 1.0);
        assert!(asset.liquidity_score >= 0.0 && asset.liquidity_score <= 1.0);
        assert!(asset.composite_score >= 0.0 && asset.composite_score <= 1.0);
    }

    // Assets should be sorted by composite score (descending)
    for i in 1..assets.len() {
        assert!(assets[i - 1].composite_score >= assets[i].composite_score);
    }

    // Cleanup
    cleanup_test_data(&selector.pool, universe_id).await?;

    Ok(())
}

#[tokio::test]
async fn test_asset_selection_persists_to_db() -> Result<()> {
    let pool = setup_test_db().await?;
    let universe_id = "test_universe_persist";

    // Seed test data
    seed_test_universe(&pool, universe_id).await?;

    let ml_strategy = Arc::new(make_test_strategy());
    let selector = AssetSelector::new(pool.clone(), ml_strategy, None)?;

    let assets = selector.select_assets(universe_id, 5).await?;

    // Verify data was persisted
    let count = sqlx::query!(
        "SELECT COUNT(*) as count FROM asset_selections WHERE universe_id = $1",
        universe_id
    )
    .fetch_one(&pool)
    .await?
    .count
    .unwrap_or(0);

    assert!(count >= assets.len() as i64);

    // Cleanup
    cleanup_test_data(&selector.pool, universe_id).await?;

    Ok(())
}

#[tokio::test]
async fn test_get_selected_assets() -> Result<()> {
    let pool = setup_test_db().await?;
    let universe_id = "test_universe_retrieve";

    // Seed test data
    seed_test_universe(&pool, universe_id).await?;

    let ml_strategy = Arc::new(make_test_strategy());
    let selector = AssetSelector::new(pool.clone(), ml_strategy, None)?;

    // First, create a selection
    let assets = selector.select_assets(universe_id, 3).await?;
    let selection_id = format!("{}_{}", universe_id, Utc::now().timestamp());

    // Retrieve the selection (this might fail if exact selection_id doesn't match)
    // In production, we'd return the selection_id from select_assets
    // For now, just verify we can query selections
    let retrieved_count = sqlx::query!(
        "SELECT COUNT(*) as count FROM asset_selections WHERE universe_id = $1",
        universe_id
    )
    .fetch_one(&pool)
    .await?
    .count
    .unwrap_or(0);

    assert!(retrieved_count >= assets.len() as i64);

    // Cleanup
    cleanup_test_data(&selector.pool, universe_id).await?;

    Ok(())
}

#[tokio::test]
async fn test_ml_integration_with_fallback() -> Result<()> {
    let pool = setup_test_db().await?;
    let universe_id = "test_ml_fallback";

    // Seed test data
    seed_test_universe(&pool, universe_id).await?;

    let ml_strategy = Arc::new(make_test_strategy());
    let selector = AssetSelector::new(pool, ml_strategy, None)?;

    // Select assets - should work even if ML predictions aren't perfect
    let assets = selector.select_assets(universe_id, 5).await?;

    // Should still return results with fallback scores
    assert!(!assets.is_empty());

    // ML scores should be present (either from ML or fallback)
    for asset in &assets {
        assert!(asset.ml_score >= 0.0 && asset.ml_score <= 1.0);
    }

    // Cleanup
    cleanup_test_data(&selector.pool, universe_id).await?;

    Ok(())
}

#[tokio::test]
async fn test_scoring_weights_affect_ranking() -> Result<()> {
    let pool = setup_test_db().await?;
    let universe_id = "test_weights_ranking";

    // Seed test data
    seed_test_universe(&pool, universe_id).await?;

    // Test with ML-heavy weights
    let ml_heavy_weights = ScoringWeights {
        ml_weight: 0.7,
        momentum_weight: 0.1,
        value_weight: 0.1,
        liquidity_weight: 0.1,
    };

    let ml_strategy1 = Arc::new(make_test_strategy());
    let selector1 = AssetSelector::new(pool.clone(), ml_strategy1, Some(ml_heavy_weights))?;
    let assets1 = selector1.select_assets(universe_id, 5).await?;

    // Test with momentum-heavy weights
    let momentum_heavy_weights = ScoringWeights {
        ml_weight: 0.1,
        momentum_weight: 0.7,
        value_weight: 0.1,
        liquidity_weight: 0.1,
    };

    let ml_strategy2 = Arc::new(make_test_strategy());
    let selector2 = AssetSelector::new(pool.clone(), ml_strategy2, Some(momentum_heavy_weights))?;
    let assets2 = selector2.select_assets(universe_id, 5).await?;

    // Rankings might differ based on weights
    // Just verify both selections work
    assert!(!assets1.is_empty());
    assert!(!assets2.is_empty());

    // Cleanup
    cleanup_test_data(&selector1.pool, universe_id).await?;

    Ok(())
}

#[tokio::test]
async fn test_ml_prediction_caching() -> Result<()> {
    let pool = setup_test_db().await?;
    let universe_id = "test_ml_cache";

    // Seed test data
    seed_test_universe(&pool, universe_id).await?;

    let ml_strategy = Arc::new(make_test_strategy());
    let selector = Arc::new(AssetSelector::new(pool, ml_strategy, None)?);

    // First selection - should query ML
    let start1 = std::time::Instant::now();
    let assets1 = selector.select_assets(universe_id, 3).await?;
    let duration1 = start1.elapsed();

    // Second selection immediately after - should use cache
    let start2 = std::time::Instant::now();
    let assets2 = selector.select_assets(universe_id, 3).await?;
    let duration2 = start2.elapsed();

    // Cached query should be faster (though this might not always hold in tests)
    println!(
        "First query: {:?}, Second query: {:?}",
        duration1, duration2
    );

    // Both should return results
    assert!(!assets1.is_empty());
    assert!(!assets2.is_empty());

    // Cleanup
    cleanup_test_data(&selector.pool, universe_id).await?;

    Ok(())
}

#[tokio::test]
async fn test_performance_target() -> Result<()> {
    let pool = setup_test_db().await?;
    let universe_id = "test_performance";

    // Seed test data with more instruments
    seed_test_universe(&pool, universe_id).await?;

    let ml_strategy = Arc::new(make_test_strategy());
    let selector = AssetSelector::new(pool, ml_strategy, None)?;

    // Measure selection time
    let start = std::time::Instant::now();
    let assets = selector.select_assets(universe_id, 10).await?;
    let duration = start.elapsed();

    println!("Selection time: {:?} for {} assets", duration, assets.len());

    // Should complete in under 2 seconds (target from requirements)
    assert!(
        duration.as_secs() < 2,
        "Selection took {:?}, expected <2s",
        duration
    );

    // Cleanup
    cleanup_test_data(&selector.pool, universe_id).await?;

    Ok(())
}

#[test]
fn test_asset_score_metadata() {
    let mut metadata = std::collections::HashMap::new();
    metadata.insert("current_price".to_string(), 42.5);
    metadata.insert("volume_24h".to_string(), 1000000.0);

    let score = AssetScore {
        symbol: "TEST".to_string(),
        ml_score: 0.8,
        momentum_score: 0.7,
        value_score: 0.6,
        liquidity_score: 0.9,
        composite_score: 0.75,
        timestamp: Utc::now(),
        metadata: metadata.clone(),
    };

    // Verify metadata is accessible
    assert_eq!(score.metadata.get("current_price"), Some(&42.5));
    assert_eq!(score.metadata.get("volume_24h"), Some(&1000000.0));
}

#[tokio::test]
async fn test_concurrent_asset_selection() -> Result<()> {
    let pool = setup_test_db().await?;
    let universe_id = "test_concurrent";

    // Seed test data
    seed_test_universe(&pool, universe_id).await?;

    let ml_strategy = Arc::new(make_test_strategy());
    let selector = Arc::new(AssetSelector::new(pool, ml_strategy, None)?);

    // Run multiple concurrent selections
    let mut handles = vec![];
    for i in 0..5 {
        let selector_clone = Arc::clone(&selector);
        let universe_id_clone = format!("{}_{}", universe_id, i);

        let handle =
            tokio::spawn(async move { selector_clone.select_assets(&universe_id_clone, 3).await });

        handles.push(handle);
    }

    // Wait for all to complete
    for handle in handles {
        let result = handle.await?;
        // Should succeed (might be empty for non-existent universes)
        assert!(result.is_ok());
    }

    // Cleanup
    cleanup_test_data(&selector.pool, universe_id).await?;

    Ok(())
}
