#![allow(unexpected_cfgs)]
#![cfg(feature = "__trading_service_integration")]
//! Unit Tests for Trading Service ML Order Functionality
//!
//! This test suite covers the core ML order submission and prediction retrieval logic.
//! Tests are designed to validate:
//! - ML order submission with ensemble predictions
//! - Single-model ML order submission
//! - ML prediction history retrieval with filtering
//! - ML model performance metrics calculation
//! - Integration with SharedMLStrategy from common crate
//!
//! Test Data Setup:
//! - Uses real PostgreSQL database with test schema
//! - Seeds ensemble_predictions table with test data
//! - Seeds ml_model_performance table with metrics
//! - Cleans up after each test

use anyhow::Result;
use chrono::Utc;
use sqlx::PgPool;
use tokio;
use tonic::Request;
use uuid::Uuid;

use trading_service::proto::trading::{
    trading_service_server::TradingService, MLOrderRequest, MLOrderResponse, MLPerformanceRequest,
    MLPerformanceResponse, MLPredictionsRequest, MLPredictionsResponse,
};
use trading_service::services::trading::TradingServiceImpl;
use trading_service::state::TradingServiceState;

/// Helper: Create test trading service instance
async fn create_test_service() -> (TradingServiceImpl, PgPool) {
    // Get database URL from environment
    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
        .expect("Failed to connect to test database");

    // Create trading service state
    let state = TradingServiceState::new_for_test(pool.clone())
        .await
        .expect("Failed to create trading service state");

    let service = TradingServiceImpl::new(std::sync::Arc::new(state));

    (service, pool)
}

/// Helper: Seed ensemble_predictions table with test data
async fn seed_ensemble_predictions(
    pool: &PgPool,
    symbol: &str,
    action: &str,
    confidence: f64,
    model_filter: Option<&str>,
) -> Uuid {
    let prediction_id = Uuid::new_v4();

    // Use model_filter to set individual model signals
    let (dqn_signal, mamba2_signal, ppo_signal, tft_signal) = match model_filter {
        Some("DQN") => (0.9, 0.5, 0.5, 0.5),
        Some("MAMBA2") => (0.5, 0.9, 0.5, 0.5),
        Some("PPO") => (0.5, 0.5, 0.9, 0.5),
        Some("TFT") => (0.5, 0.5, 0.5, 0.9),
        _ => (0.7, 0.7, 0.7, 0.7), // Ensemble
    };

    sqlx::query!(
        r#"
        INSERT INTO ensemble_predictions (
            id, symbol, ensemble_action, ensemble_signal, ensemble_confidence,
            dqn_signal, dqn_confidence, mamba2_signal, mamba2_confidence,
            ppo_signal, ppo_confidence, tft_signal, tft_confidence,
            account_id, timestamp
        ) VALUES (
            $1, $2, $3, $4, $5,
            $6, 0.7, $7, 0.7,
            $8, 0.7, $9, 0.7,
            'test_account', NOW()
        )
        "#,
        prediction_id,
        symbol,
        action,
        confidence,
        confidence,
        dqn_signal,
        mamba2_signal,
        ppo_signal,
        tft_signal,
    )
    .execute(pool)
    .await
    .expect("Failed to seed ensemble_predictions");

    prediction_id
}

/// Helper: Seed ml_model_performance table with deterministic metrics
async fn seed_model_performance(
    pool: &PgPool,
    model_name: &str,
    total_predictions: i32,
    correct_predictions: i32,
    avg_pnl: f64,
    sharpe_ratio: f64,
) {
    let accuracy = if total_predictions > 0 {
        (correct_predictions as f64 / total_predictions as f64) * 100.0
    } else {
        0.0
    };

    sqlx::query!(
        r#"
        INSERT INTO ml_model_performance (
            model_name, total_predictions, predictions_with_outcomes,
            correct_predictions, accuracy, avg_pnl, sharpe_ratio
        ) VALUES (
            $1, $2, $2, $3, $4, $5, $6
        )
        ON CONFLICT (model_name) DO UPDATE SET
            total_predictions = EXCLUDED.total_predictions,
            predictions_with_outcomes = EXCLUDED.predictions_with_outcomes,
            correct_predictions = EXCLUDED.correct_predictions,
            accuracy = EXCLUDED.accuracy,
            avg_pnl = EXCLUDED.avg_pnl,
            sharpe_ratio = EXCLUDED.sharpe_ratio
        "#,
        model_name,
        total_predictions,
        correct_predictions,
        accuracy,
        avg_pnl,
        sharpe_ratio,
    )
    .execute(pool)
    .await
    .expect("Failed to seed ml_model_performance");
}

/// Helper: Clean up test data
async fn cleanup_test_data(pool: &PgPool, prediction_ids: &[Uuid]) {
    for id in prediction_ids {
        let _ = sqlx::query!("DELETE FROM ensemble_predictions WHERE id = $1", id)
            .execute(pool)
            .await;
    }
}

// ============================================================================
// TEST 1: ML Order Submission with Ensemble Voting
// ============================================================================

#[tokio::test]
async fn test_ml_order_submission_ensemble() -> Result<()> {
    let (service, pool) = create_test_service().await;

    // Arrange: Create 26 features (OHLCV + 21 technical indicators)
    let features: Vec<f64> = vec![
        // OHLCV (5 features)
        4500.0, 4510.0, 4490.0, 4505.0, 100000.0, // Technical indicators (21 features)
        0.65, 0.70, 0.75, 0.80, 0.85, // Strong bullish signals
        4520.0, 4480.0, // Bollinger bands (wide)
        120.0,  // ATR (high volatility)
        4490.0, 4500.0, 4510.0, // EMAs (trending up)
        0.70, 0.75, 0.80, // Additional bullish indicators
        1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, // More features
    ];

    let request = Request::new(MLOrderRequest {
        symbol: "ES.FUT".to_string(),
        account_id: "test_account".to_string(),
        use_ensemble: true,
        model_name: None,
        features,
    });

    // Act
    let response: tonic::Response<MLOrderResponse> = service.submit_ml_order(request).await?;
    let ml_order = response.into_inner();

    // Assert: Verify ensemble vote was calculated
    assert!(
        !ml_order.order_id.is_empty(),
        "Order ID should not be empty"
    );
    assert!(
        !ml_order.prediction_id.is_empty(),
        "Prediction ID should not be empty"
    );
    assert!(
        ml_order.action == "BUY" || ml_order.action == "SELL" || ml_order.action == "HOLD",
        "Action should be BUY, SELL, or HOLD, got: {}",
        ml_order.action
    );
    assert!(
        ml_order.confidence > 0.0 && ml_order.confidence <= 1.0,
        "Confidence should be 0-1, got: {}",
        ml_order.confidence
    );

    // Verify prediction stored in database
    if let Ok(pred_id) = Uuid::parse_str(&ml_order.prediction_id) {
        let stored = sqlx::query!(
            "SELECT id, ensemble_action FROM ensemble_predictions WHERE id = $1",
            pred_id
        )
        .fetch_optional(&pool)
        .await?;

        assert!(stored.is_some(), "Prediction should be stored in database");
        cleanup_test_data(&pool, &[pred_id]).await;
    }

    Ok(())
}

// ============================================================================
// TEST 2: ML Order Submission with Single Model Filter
// ============================================================================

#[tokio::test]
async fn test_ml_order_submission_single_model() -> Result<()> {
    let (service, pool) = create_test_service().await;

    // Arrange: DQN-specific features
    let features: Vec<f64> = vec![
        4500.0, 4510.0, 4490.0, 4505.0, 100000.0, 0.6, 0.7, 0.8, 0.85, 0.9, 4520.0, 4480.0, 120.0,
        4490.0, 4500.0, 4510.0, 0.7, 0.75, 0.8, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8,
    ];

    let request = Request::new(MLOrderRequest {
        symbol: "ES.FUT".to_string(),
        account_id: "test_account".to_string(),
        use_ensemble: false,
        model_name: Some("DQN".to_string()),
        features,
    });

    // Act
    let response = service.submit_ml_order(request).await?;
    let ml_order = response.into_inner();

    // Assert: Verify correct model used (stored in prediction metadata or logs)
    assert!(
        !ml_order.prediction_id.is_empty(),
        "Prediction ID should exist"
    );

    // Query database to verify DQN signal is dominant
    if let Ok(pred_id) = Uuid::parse_str(&ml_order.prediction_id) {
        let stored = sqlx::query!(
            r#"
            SELECT dqn_signal, dqn_confidence, mamba2_signal, ppo_signal
            FROM ensemble_predictions WHERE id = $1
            "#,
            pred_id
        )
        .fetch_optional(&pool)
        .await?;

        if let Some(pred) = stored {
            // For single-model submission, DQN should be used
            // (implementation detail: may set DQN signal higher or only use DQN)
            assert!(
                pred.dqn_signal.unwrap_or(0.0) >= 0.0,
                "DQN signal should be set"
            );
        }

        cleanup_test_data(&pool, &[pred_id]).await;
    }

    Ok(())
}

// ============================================================================
// TEST 3: Get ML Predictions with Model Filter
// ============================================================================

#[tokio::test]
async fn test_get_ml_predictions_filtering() -> Result<()> {
    let (service, pool) = create_test_service().await;

    // Arrange: Seed predictions with different model dominance
    let pred1 = seed_ensemble_predictions(&pool, "ES.FUT", "BUY", 0.85, Some("DQN")).await;
    let pred2 = seed_ensemble_predictions(&pool, "ES.FUT", "SELL", 0.75, Some("DQN")).await;
    let pred3 = seed_ensemble_predictions(&pool, "ES.FUT", "BUY", 0.90, Some("MAMBA2")).await;

    // Act: Query with implicit DQN filter (filter by high DQN signal)
    let request = Request::new(MLPredictionsRequest {
        symbol: "ES.FUT".to_string(),
        model_name: Some("DQN".to_string()),
        limit: 100,
        start_time: None,
        end_time: None,
    });

    let response: tonic::Response<MLPredictionsResponse> =
        service.get_ml_predictions(request).await?;
    let predictions = response.into_inner();

    // Assert: Should return predictions (filtering by model may be optional)
    assert!(
        predictions.predictions.len() >= 2,
        "Should return at least 2 predictions for ES.FUT"
    );

    // Verify all predictions are for ES.FUT
    assert!(
        predictions.predictions.iter().all(|p| p.symbol == "ES.FUT"),
        "All predictions should be for ES.FUT"
    );

    // Cleanup
    cleanup_test_data(&pool, &[pred1, pred2, pred3]).await;

    Ok(())
}

// ============================================================================
// TEST 4: ML Performance Calculation
// ============================================================================

#[tokio::test]
async fn test_ml_performance_calculation() -> Result<()> {
    let (service, pool) = create_test_service().await;

    // Arrange: Seed predictions with known outcomes (65% win rate for DQN)
    seed_model_performance(&pool, "DQN", 100, 65, 1250.0, 1.85).await;

    let request = Request::new(MLPerformanceRequest {
        model_name: Some("DQN".to_string()),
        start_time: None,
        end_time: None,
    });

    // Act
    let response: tonic::Response<MLPerformanceResponse> =
        service.get_ml_performance(request).await?;
    let performance = response.into_inner();

    // Assert: Verify calculated metrics
    assert_eq!(
        performance.models.len(),
        1,
        "Should return exactly 1 model (DQN)"
    );

    let dqn_perf = &performance.models[0];
    assert_eq!(dqn_perf.model_name, "DQN");
    assert_eq!(dqn_perf.total_predictions, 100);
    assert!(
        (dqn_perf.accuracy - 65.0).abs() < 1.0,
        "Accuracy should be ~65%, got: {}",
        dqn_perf.accuracy
    );
    assert!(
        (dqn_perf.sharpe_ratio - 1.85).abs() < 0.1,
        "Sharpe ratio should be ~1.85, got: {}",
        dqn_perf.sharpe_ratio
    );

    Ok(())
}

// ============================================================================
// TEST 5: ML Performance All Models
// ============================================================================

#[tokio::test]
async fn test_ml_performance_all_models() -> Result<()> {
    let (service, pool) = create_test_service().await;

    // Arrange: Seed performance for all 4 models
    seed_model_performance(&pool, "DQN", 100, 65, 1250.0, 1.85).await;
    seed_model_performance(&pool, "MAMBA2", 120, 84, 1680.0, 2.10).await;
    seed_model_performance(&pool, "PPO", 90, 54, 900.0, 1.50).await;
    seed_model_performance(&pool, "TFT", 110, 77, 1540.0, 1.95).await;

    let request = Request::new(MLPerformanceRequest {
        model_name: None, // Get all models
        start_time: None,
        end_time: None,
    });

    // Act
    let response = service.get_ml_performance(request).await?;
    let performance = response.into_inner();

    // Assert: All models should be returned
    assert!(
        performance.models.len() >= 4,
        "Should return at least 4 models, got: {}",
        performance.models.len()
    );

    // Verify each model exists
    let model_names: Vec<&str> = performance
        .models
        .iter()
        .map(|m| m.model_name.as_str())
        .collect();
    assert!(model_names.contains(&"DQN"), "Should include DQN");
    assert!(model_names.contains(&"MAMBA2"), "Should include MAMBA2");
    assert!(model_names.contains(&"PPO"), "Should include PPO");
    assert!(model_names.contains(&"TFT"), "Should include TFT");

    // Verify MAMBA2 has best metrics
    let mamba2 = performance
        .models
        .iter()
        .find(|m| m.model_name == "MAMBA2")
        .expect("MAMBA2 should exist");
    assert!(
        (mamba2.accuracy - 70.0).abs() < 1.0,
        "MAMBA2 accuracy should be ~70%"
    );
    assert!(
        (mamba2.sharpe_ratio - 2.10).abs() < 0.1,
        "MAMBA2 Sharpe should be ~2.10"
    );

    Ok(())
}

// ============================================================================
// TEST 6: SharedMLStrategy Integration Test
// ============================================================================

#[tokio::test]
async fn test_shared_ml_strategy_integration() -> Result<()> {
    use common::ml_strategy::{
        MLModelAdapter, MLPrediction, ProductionFeatureExtractor225, SharedMLStrategy,
    };

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

    struct MockAdapter;
    impl MLModelAdapter for MockAdapter {
        fn predict(&self, features: &[f64]) -> Result<MLPrediction> {
            let sum: f64 = features.iter().sum::<f64>() / features.len().max(1) as f64;
            let pv = 1.0 / (1.0 + (-sum).exp());
            Ok(MLPrediction {
                model_id: "mock_v1".to_string(),
                prediction_value: pv,
                confidence: 0.5 + (pv - 0.5).abs() * 0.8,
                features: features.to_vec(),
                timestamp: Utc::now(),
                inference_latency_us: 10,
            })
        }
        fn model_id(&self) -> &str {
            "mock_v1"
        }
        fn validate_prediction(&mut self, _prediction: &MLPrediction, _actual_outcome: bool) {}
    }

    // Arrange: Create strategy with mock adapter and 60% confidence threshold
    let strategy = SharedMLStrategy::new(
        Box::new(MockExtractor),
        vec![Box::new(MockAdapter)],
        0.6,
    );

    // Act: Get ensemble prediction with realistic market data
    let predictions = strategy
        .get_ensemble_prediction(
            4500.0,   // price
            100000.0, // volume
            Utc::now(),
        )
        .await?;

    // Assert: Should have at least 1 prediction from injected model adapter
    assert!(
        !predictions.is_empty(),
        "Should return at least 1 prediction from model adapter"
    );

    // Calculate ensemble vote
    let vote_result = strategy.calculate_ensemble_vote(&predictions);
    assert!(
        vote_result.is_some(),
        "Should calculate ensemble vote from predictions"
    );

    let (vote, confidence) = vote_result.unwrap();

    // Verify vote and confidence ranges
    assert!(
        vote >= 0.0 && vote <= 1.0,
        "Vote should be 0-1, got: {}",
        vote
    );
    assert!(
        confidence >= 0.0 && confidence <= 1.0,
        "Confidence should be 0-1, got: {}",
        confidence
    );

    Ok(())
}
