#![allow(
    clippy::tests_outside_test_module,
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::indexing_slicing,
    clippy::str_to_string,
    clippy::string_to_string,
    clippy::assertions_on_result_states,
    clippy::assertions_on_constants,
    clippy::let_underscore_must_use,
    clippy::use_debug,
    clippy::doc_markdown,
    clippy::shadow_unrelated,
    clippy::shadow_reuse,
    clippy::similar_names,
    clippy::clone_on_copy,
    clippy::get_unwrap,
    clippy::modulo_arithmetic,
    clippy::integer_division,
    clippy::non_ascii_literal,
    clippy::useless_vec,
    clippy::useless_format,
    clippy::wildcard_enum_match_arm,
    clippy::manual_range_contains,
    clippy::const_is_empty,
    clippy::needless_range_loop,
    clippy::field_reassign_with_default,
    clippy::items_after_test_module,
    clippy::missing_const_for_fn,
    unused_imports,
    unused_variables,
    unused_mut,
    unused_assignments,
    unused_comparisons,
    unused_must_use,
    dead_code,
)]
//! Core Trading Engine Integration Tests
//!
//! Comprehensive integration tests for trading engine core functionality:
//! - Full order flow validation
//! - Lock-free queue under contention
//! - Position manager state consistency
//! - Risk manager integration with compliance

use common::{Execution as ExecutionReport, Position};
use common::{OrderId, OrderSide, OrderStatus, OrderType, TimeInForce};
use rust_decimal::prelude::ToPrimitive;
use rust_decimal::Decimal;
use std::str::FromStr;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::thread;
use std::time::Instant;
use trading_engine::lockfree::ring_buffer::LockFreeRingBuffer;
use trading_engine::trading::data_interface::{
    BrokerConnectionStatus, BrokerError, BrokerInterface, DataProvider, Subscription,
};
use trading_engine::trading::engine::TradingEngine;
use trading_engine::trading::order_manager::OrderManager;
use trading_engine::trading::position_manager::PositionManager;
use trading_engine::trading_operations::{ExecutionResult, LiquidityFlag, TradingOrder};

// ============================================================================
// Mock Data Provider for Testing
// ============================================================================

#[derive(Debug, Clone)]
struct MockDataProvider {
    market_data_tx: Arc<tokio::sync::broadcast::Sender<common::MarketDataEvent>>,
    order_update_tx: Arc<tokio::sync::broadcast::Sender<common::MarketDataEvent>>,
}

impl MockDataProvider {
    fn new() -> Self {
        let (market_data_tx, _) = tokio::sync::broadcast::channel(100);
        let (order_update_tx, _) = tokio::sync::broadcast::channel(100);
        Self {
            market_data_tx: Arc::new(market_data_tx),
            order_update_tx: Arc::new(order_update_tx),
        }
    }
}

#[async_trait::async_trait]
impl DataProvider for MockDataProvider {
    async fn subscribe_market_data(&self, _subscription: Subscription) -> Result<(), String> {
        Ok(())
    }

    fn subscribe_market_data_events(
        &self,
    ) -> tokio::sync::broadcast::Receiver<common::MarketDataEvent> {
        self.market_data_tx.subscribe()
    }

    fn subscribe_order_update_events(
        &self,
    ) -> tokio::sync::broadcast::Receiver<common::MarketDataEvent> {
        self.order_update_tx.subscribe()
    }
}

// ============================================================================
// Mock Broker for Testing
// ============================================================================

#[derive(Debug, Clone)]
struct TestBroker;

#[async_trait::async_trait]
impl BrokerInterface for TestBroker {
    async fn connect(&mut self) -> Result<(), BrokerError> {
        Ok(())
    }

    async fn disconnect(&mut self) -> Result<(), BrokerError> {
        Ok(())
    }

    fn is_connected(&self) -> bool {
        true
    }

    fn connection_status(&self) -> BrokerConnectionStatus {
        BrokerConnectionStatus::Connected
    }

    async fn submit_order(&self, order: &TradingOrder) -> Result<String, BrokerError> {
        Ok(format!("TEST-{}", order.id))
    }

    async fn cancel_order(&self, _order_id: &str) -> Result<(), BrokerError> {
        Ok(())
    }

    async fn modify_order(
        &self,
        _order_id: &str,
        _order: &TradingOrder,
    ) -> Result<(), BrokerError> {
        Ok(())
    }

    async fn get_order_status(&self, _order_id: &str) -> Result<OrderStatus, BrokerError> {
        Ok(OrderStatus::Filled)
    }

    async fn get_account_info(
        &self,
    ) -> Result<std::collections::HashMap<String, String>, BrokerError> {
        Ok(std::collections::HashMap::new())
    }

    async fn get_positions(&self) -> Result<Vec<Position>, BrokerError> {
        Ok(Vec::new())
    }

    async fn subscribe_executions(
        &self,
    ) -> Result<tokio::sync::mpsc::Receiver<ExecutionReport>, BrokerError> {
        let (_tx, rx) = tokio::sync::mpsc::channel(1);
        Ok(rx)
    }

    fn broker_name(&self) -> &str {
        "test_broker"
    }

    async fn send_heartbeat(&self) -> Result<(), BrokerError> {
        Ok(())
    }

    async fn reconnect(&self) -> Result<(), BrokerError> {
        Ok(())
    }
}

// ============================================================================
// Helper Functions
// ============================================================================

async fn create_test_engine() -> TradingEngine {
    let data_provider = Arc::new(MockDataProvider::new());
    let engine = TradingEngine::new(data_provider);

    // Configure the broker client with a test broker
    engine
        .broker_client()
        .add_broker_for_tests("test_broker".to_owned(), Box::new(TestBroker))
        .await
        .expect("Failed to add test broker");

    engine
}

fn create_test_order(symbol: &str, side: OrderSide, quantity: f64, price: f64) -> TradingOrder {
    TradingOrder {
        id: OrderId::new(),
        symbol: symbol.to_owned(),
        side,
        order_type: OrderType::Limit,
        quantity: Decimal::from_str(&quantity.to_string()).unwrap(),
        price: Decimal::from_str(&price.to_string()).unwrap(),
        time_in_force: TimeInForce::Day,
        account_id: None,
        metadata: std::collections::HashMap::new(),
        created_at: chrono::Utc::now(),
        submitted_at: None,
        executed_at: None,
        status: OrderStatus::Created,
        fill_quantity: Decimal::ZERO,
        average_fill_price: None,
    }
}

fn create_test_execution(symbol: &str, quantity: f64, price: f64) -> ExecutionResult {
    ExecutionResult {
        order_id: OrderId::new(),
        symbol: symbol.to_owned(),
        side: OrderSide::Buy,
        executed_quantity: Decimal::from_str(&quantity.to_string()).unwrap(),
        execution_price: Decimal::from_str(&price.to_string()).unwrap(),
        execution_time: chrono::Utc::now(),
        commission: Decimal::ZERO,
        liquidity_flag: LiquidityFlag::Taker,
    }
}

// ============================================================================
// Order Flow Pipeline Tests (15+ tests)
// ============================================================================

#[cfg(test)]
mod order_flow_tests {
    use super::*;

    #[tokio::test]
    async fn test_market_order_submission() {
        let engine = create_test_engine().await;
        let result = engine
            .submit_order(
                "BTC-USD".to_owned(),
                OrderSide::Buy,
                OrderType::Market,
                Decimal::from_str("1.0").unwrap(),
                None,
                None,
            )
            .await;
        assert!(
            result.is_ok(),
            "Market order submission should succeed: {:?}",
            result.err()
        );
    }

    #[tokio::test]
    async fn test_limit_order_submission() {
        let engine = create_test_engine().await;
        let result = engine
            .submit_order(
                "ETH-USD".to_owned(),
                OrderSide::Sell,
                OrderType::Limit,
                Decimal::from_str("10.0").unwrap(),
                Some(Decimal::from_str("2000.0").unwrap()),
                None,
            )
            .await;
        assert!(result.is_ok(), "Limit order submission should succeed");
    }

    #[tokio::test]
    async fn test_stop_order_submission() {
        let engine = create_test_engine().await;
        let result = engine
            .submit_order(
                "SOL-USD".to_owned(),
                OrderSide::Buy,
                OrderType::Stop,
                Decimal::from_str("5.0").unwrap(),
                None,
                Some(Decimal::from_str("100.0").unwrap()),
            )
            .await;
        assert!(result.is_ok(), "Stop order submission should succeed");
    }

    #[tokio::test]
    async fn test_stop_limit_order_submission() {
        let engine = create_test_engine().await;
        let result = engine
            .submit_order(
                "AVAX-USD".to_owned(),
                OrderSide::Sell,
                OrderType::StopLimit,
                Decimal::from_str("20.0").unwrap(),
                Some(Decimal::from_str("35.0").unwrap()),
                Some(Decimal::from_str("36.0").unwrap()),
            )
            .await;
        assert!(result.is_ok(), "Stop-limit order submission should succeed");
    }

    #[tokio::test]
    async fn test_order_validation_invalid_quantity() {
        let order_manager = OrderManager::new();
        let order = create_test_order("BTC-USD", OrderSide::Buy, 0.0, 50000.0);
        let result = order_manager.validate_order(&order).await;
        assert!(result.is_err(), "Should reject zero quantity");
        assert!(result.unwrap_err().contains("quantity"));
    }

    #[tokio::test]
    async fn test_order_validation_invalid_price() {
        let order_manager = OrderManager::new();
        let order = create_test_order("ETH-USD", OrderSide::Buy, 1.0, 0.0);
        let result = order_manager.validate_order(&order).await;
        assert!(result.is_err(), "Should reject zero price for limit order");
        assert!(result.unwrap_err().contains("price"));
    }

    #[tokio::test]
    async fn test_order_validation_empty_symbol() {
        let order_manager = OrderManager::new();
        let order = create_test_order("", OrderSide::Buy, 1.0, 50000.0);
        let result = order_manager.validate_order(&order).await;
        assert!(result.is_err(), "Should reject empty symbol");
        assert!(result.unwrap_err().contains("symbol"));
    }

    #[tokio::test]
    async fn test_order_validation_duplicate_id() {
        let order_manager = OrderManager::new();
        let mut order1 = create_test_order("BTC-USD", OrderSide::Buy, 1.0, 50000.0);
        let order_id = OrderId::new();
        order1.id = order_id;

        order_manager.add_order(order1.clone()).await;

        let mut order2 = create_test_order("ETH-USD", OrderSide::Sell, 2.0, 3000.0);
        order2.id = order_id; // Same ID

        let result = order_manager.validate_order(&order2).await;
        assert!(result.is_err(), "Should reject duplicate order ID");
    }

    #[tokio::test]
    async fn test_concurrent_order_submission_100_orders() {
        let engine = Arc::new(create_test_engine().await);
        let mut handles = vec![];

        for i in 0..100 {
            let engine_clone = Arc::clone(&engine);
            let handle = tokio::spawn(async move {
                engine_clone
                    .submit_order(
                        format!("TEST-{}", i % 10),
                        if i % 2 == 0 {
                            OrderSide::Buy
                        } else {
                            OrderSide::Sell
                        },
                        OrderType::Limit,
                        Decimal::from_str(&format!("{}.0", i % 10 + 1)).unwrap(),
                        Some(Decimal::from_str(&format!("{}.0", 1000 + i)).unwrap()),
                        None,
                    )
                    .await
            });
            handles.push(handle);
        }

        let results: Vec<_> = futures::future::join_all(handles).await;
        let success_count = results
            .iter()
            .filter(|r| r.is_ok() && r.as_ref().unwrap().is_ok())
            .count();

        assert!(
            success_count >= 95,
            "At least 95% of concurrent orders should succeed, got {}",
            success_count
        );
    }

    #[tokio::test]
    async fn test_order_status_transitions() {
        let order_manager = OrderManager::new();
        let order = create_test_order("BTC-USD", OrderSide::Buy, 1.0, 50000.0);
        let order_id = order.id;

        order_manager.add_order(order).await;

        // Created -> Pending
        let result = order_manager
            .update_order_status(&order_id, OrderStatus::Pending)
            .await;
        result.unwrap();

        let order = order_manager.get_order(&order_id).await.unwrap();
        assert_eq!(order.status, OrderStatus::Pending);

        // Pending -> Filled
        let result = order_manager
            .update_order_status(&order_id, OrderStatus::Filled)
            .await;
        result.unwrap();

        let order = order_manager.get_order(&order_id).await.unwrap();
        assert_eq!(order.status, OrderStatus::Filled);
    }

    #[tokio::test]
    async fn test_order_rejection_scenarios() {
        let engine = create_test_engine().await;

        // Test negative quantity (should be rejected)
        let _result = engine
            .submit_order(
                "BTC-USD".to_owned(),
                OrderSide::Buy,
                OrderType::Limit,
                Decimal::from_str("-1.0").unwrap(),
                Some(Decimal::from_str("50000.0").unwrap()),
                None,
            )
            .await;
        // Note: The engine may accept this but validation should catch it
        // This tests that the system handles invalid input gracefully
    }

    #[tokio::test]
    async fn test_order_buy_side() {
        let engine = create_test_engine().await;
        let result = engine
            .submit_order(
                "BTC-USD".to_owned(),
                OrderSide::Buy,
                OrderType::Market,
                Decimal::from_str("0.5").unwrap(),
                None,
                None,
            )
            .await;
        assert!(result.is_ok(), "Buy order should succeed");
    }

    #[tokio::test]
    async fn test_order_sell_side() {
        let engine = create_test_engine().await;
        let result = engine
            .submit_order(
                "ETH-USD".to_owned(),
                OrderSide::Sell,
                OrderType::Market,
                Decimal::from_str("2.0").unwrap(),
                None,
                None,
            )
            .await;
        assert!(result.is_ok(), "Sell order should succeed");
    }

    #[tokio::test]
    async fn test_order_with_different_quantities() {
        let engine = create_test_engine().await;

        let quantities = vec!["0.001", "1.0", "100.0", "1000.0"];

        for qty in quantities {
            let result = engine
                .submit_order(
                    "BTC-USD".to_owned(),
                    OrderSide::Buy,
                    OrderType::Market,
                    Decimal::from_str(qty).unwrap(),
                    None,
                    None,
                )
                .await;
            assert!(result.is_ok(), "Order with quantity {} should succeed", qty);
        }
    }

    #[tokio::test]
    async fn test_order_pipeline_end_to_end() {
        let engine = create_test_engine().await;

        // Submit order
        let result = engine
            .submit_order(
                "BTC-USD".to_owned(),
                OrderSide::Buy,
                OrderType::Limit,
                Decimal::from_str("1.0").unwrap(),
                Some(Decimal::from_str("50000.0").unwrap()),
                None,
            )
            .await;

        assert!(result.is_ok());
        let order_id = result.unwrap();
        assert!(!order_id.is_empty(), "Order ID should not be empty");
    }
}

// ============================================================================
// Lock-Free Queue Tests (10+ tests)
// ============================================================================

#[cfg(test)]
mod lockfree_queue_tests {
    use super::*;

    #[test]
    fn test_ring_buffer_basic_operations() {
        let buffer = LockFreeRingBuffer::<u64>::new(16).unwrap();

        // Test push
        buffer.try_push(42).unwrap();
        buffer.try_push(100).unwrap();

        // Test pop
        assert_eq!(buffer.try_pop(), Some(42));
        assert_eq!(buffer.try_pop(), Some(100));
        assert_eq!(buffer.try_pop(), None);
    }

    #[test]
    fn test_ring_buffer_capacity_validation() {
        // Should fail for non-power-of-2
        LockFreeRingBuffer::<u32>::new(15).unwrap_err();

        // Should succeed for power-of-2
        LockFreeRingBuffer::<u32>::new(16).unwrap();
        LockFreeRingBuffer::<u32>::new(32).unwrap();
    }

    #[test]
    fn test_ring_buffer_full_scenario() {
        let buffer = LockFreeRingBuffer::<u32>::new(4).unwrap();

        // Fill buffer
        buffer.try_push(1).unwrap();
        buffer.try_push(2).unwrap();
        buffer.try_push(3).unwrap();

        // Buffer should be full (capacity - 1 for SPSC)
        assert!(buffer.try_push(4).is_err());
    }

    #[test]
    fn test_ring_buffer_empty_scenario() {
        let buffer = LockFreeRingBuffer::<i32>::new(8).unwrap();

        // Empty buffer
        assert_eq!(buffer.try_pop(), None);

        // Add and remove one
        buffer.try_push(42).unwrap();
        assert_eq!(buffer.try_pop(), Some(42));

        // Empty again
        assert_eq!(buffer.try_pop(), None);
    }

    #[test]
    fn test_ring_buffer_multiple_producers() {
        let buffer = Arc::new(LockFreeRingBuffer::<u64>::new(1024).unwrap());
        let mut handles = vec![];

        // 10 producer threads
        for thread_id in 0..10 {
            let buffer_clone = Arc::clone(&buffer);
            let handle = thread::spawn(move || {
                for i in 0..100 {
                    let value = (thread_id * 1000 + i) as u64;
                    while buffer_clone.try_push(value).is_err() {
                        thread::yield_now();
                    }
                }
            });
            handles.push(handle);
        }

        for handle in handles {
            handle.join().unwrap();
        }

        // Verify all items were added
        let mut count = 0;
        while buffer.try_pop().is_some() {
            count += 1;
        }
        assert_eq!(
            count, 1000,
            "Should have 1000 items (10 threads * 100 items)"
        );
    }

    #[test]
    fn test_ring_buffer_multiple_consumers() {
        let buffer = Arc::new(LockFreeRingBuffer::<u64>::new(1024).unwrap());

        // Fill buffer
        for i in 0..500 {
            buffer.try_push(i).unwrap();
        }

        let mut handles = vec![];
        let consumed_count = Arc::new(AtomicU64::new(0));

        // 10 consumer threads
        for _ in 0..10 {
            let buffer_clone = Arc::clone(&buffer);
            let count_clone = Arc::clone(&consumed_count);
            let handle = thread::spawn(move || {
                let mut local_count = 0;
                while buffer_clone.try_pop().is_some() {
                    local_count += 1;
                }
                count_clone.fetch_add(local_count, Ordering::SeqCst);
            });
            handles.push(handle);
        }

        for handle in handles {
            handle.join().unwrap();
        }

        assert_eq!(
            consumed_count.load(Ordering::SeqCst),
            500,
            "Should consume all 500 items"
        );
    }

    #[test]
    fn test_ring_buffer_high_contention() {
        let buffer = Arc::new(LockFreeRingBuffer::<u64>::new(2048).unwrap());
        let mut handles = vec![];

        // 10 producers + 10 consumers
        for thread_id in 0..10 {
            let buffer_clone = Arc::clone(&buffer);
            let handle = thread::spawn(move || {
                for i in 0..1000 {
                    let value = (thread_id * 10000 + i) as u64;
                    while buffer_clone.try_push(value).is_err() {
                        thread::yield_now();
                    }
                }
            });
            handles.push(handle);
        }

        let consumed = Arc::new(AtomicU64::new(0));
        for _ in 0..10 {
            let buffer_clone = Arc::clone(&buffer);
            let consumed_clone = Arc::clone(&consumed);
            let handle = thread::spawn(move || {
                for _ in 0..1000 {
                    while buffer_clone.try_pop().is_none() {
                        thread::yield_now();
                    }
                    consumed_clone.fetch_add(1, Ordering::SeqCst);
                }
            });
            handles.push(handle);
        }

        for handle in handles {
            handle.join().unwrap();
        }

        assert_eq!(
            consumed.load(Ordering::SeqCst),
            10000,
            "All items should be consumed"
        );
    }

    #[test]
    fn test_ring_buffer_performance_under_1us() {
        let buffer = LockFreeRingBuffer::<u64>::new(1024).unwrap();

        let start = Instant::now();
        let iterations = 10000;

        for i in 0..iterations {
            buffer.try_push(i).unwrap_or_else(|_| {
                buffer.try_pop();
                buffer.try_push(i).unwrap()
            });
        }

        let elapsed = start.elapsed();
        let avg_ns = elapsed.as_nanos() / iterations as u128;

        assert!(
            avg_ns < 1000,
            "Average operation should be < 1\u{3bc}s, got {}ns",
            avg_ns
        );
    }

    #[test]
    fn test_ring_buffer_wraparound() {
        let buffer = LockFreeRingBuffer::<u32>::new(8).unwrap();

        // Fill and drain multiple times to test wraparound
        for round in 0..10 {
            for i in 0..5 {
                buffer.try_push(round * 100 + i).unwrap();
            }

            for i in 0..5 {
                assert_eq!(buffer.try_pop(), Some(round * 100 + i));
            }
        }
    }

    #[test]
    fn test_ring_buffer_zero_capacity_rejection() {
        let result = LockFreeRingBuffer::<i32>::new(0);
        assert!(result.is_err(), "Should reject zero capacity");
    }
}

// ============================================================================
// Position Manager Tests (12+ tests)
// ============================================================================

#[cfg(test)]
mod position_manager_tests {
    use super::*;

    #[test]
    fn test_position_opening_long() {
        let position_manager = PositionManager::new();
        let execution = create_test_execution("BTC-USD", 1.0, 50000.0);

        let result = position_manager.update_position(&execution);
        assert!(result.is_ok(), "Long position should open successfully");
    }

    #[test]
    fn test_position_opening_short() {
        let position_manager = PositionManager::new();
        let execution = create_test_execution("ETH-USD", -5.0, 3000.0);

        let result = position_manager.update_position(&execution);
        assert!(result.is_ok(), "Short position should open successfully");
    }

    #[test]
    fn test_position_closing_full() {
        let position_manager = PositionManager::new();

        // Open position
        let open_execution = create_test_execution("SOL-USD", 10.0, 100.0);
        position_manager.update_position(&open_execution).unwrap();

        // Close position
        let close_execution = create_test_execution("SOL-USD", -10.0, 105.0);
        let result = position_manager.update_position(&close_execution);
        assert!(result.is_ok(), "Full position close should succeed");
    }

    #[test]
    fn test_position_closing_partial() {
        let position_manager = PositionManager::new();

        // Open position
        let open_execution = create_test_execution("AVAX-USD", 20.0, 35.0);
        position_manager.update_position(&open_execution).unwrap();

        // Partial close
        let partial_close = create_test_execution("AVAX-USD", -8.0, 37.0);
        let result = position_manager.update_position(&partial_close);
        assert!(result.is_ok(), "Partial position close should succeed");
    }

    #[test]
    fn test_position_updates_on_price_change() {
        let position_manager = PositionManager::new();

        // Open position at price 1
        let execution1 = create_test_execution("TEST-USD", 100.0, 50.0);
        position_manager.update_position(&execution1).unwrap();

        // Update at price 2
        let execution2 = create_test_execution("TEST-USD", 50.0, 55.0);
        position_manager.update_position(&execution2).unwrap();

        // Verify position tracking
        let positions = position_manager.get_positions(None).unwrap();
        let has_position = positions.iter().any(|p| p.symbol == "TEST-USD");
        assert!(has_position, "Position should exist");
    }

    #[test]
    fn test_position_pnl_calculation_realized() {
        let position_manager = PositionManager::new();

        // Buy at 100
        let buy = create_test_execution("BTC-USD", 1.0, 100.0);
        position_manager.update_position(&buy).unwrap();

        // Sell at 110 (10 profit)
        let sell = create_test_execution("BTC-USD", -1.0, 110.0);
        position_manager.update_position(&sell).unwrap();

        // PnL should be tracked
        let positions = position_manager
            .get_positions(Some("BTC-USD".to_owned()))
            .unwrap();
        if let Some(position) = positions.first() {
            // Realized PnL should be tracked (can be positive or negative)
            // Just verify the field exists
            let _ = position.realized_pnl;
        }
    }

    #[test]
    fn test_position_pnl_calculation_unrealized() {
        let position_manager = PositionManager::new();

        // Open position
        let execution = create_test_execution("ETH-USD", 10.0, 3000.0);
        position_manager.update_position(&execution).unwrap();

        // Update market price (simulated through another execution)
        let price_update = create_test_execution("ETH-USD", 1.0, 3100.0);
        position_manager.update_position(&price_update).unwrap();

        // Unrealized PnL should exist
        let positions = position_manager
            .get_positions(Some("ETH-USD".to_owned()))
            .unwrap();
        assert!(!positions.is_empty(), "ETH-USD position should exist");
    }

    #[test]
    fn test_position_multi_asset_tracking() {
        let position_manager = PositionManager::new();

        // Open positions in multiple assets
        let btc = create_test_execution("BTC-USD", 1.0, 50000.0);
        let eth = create_test_execution("ETH-USD", 10.0, 3000.0);
        let sol = create_test_execution("SOL-USD", 100.0, 100.0);

        position_manager.update_position(&btc).unwrap();
        position_manager.update_position(&eth).unwrap();
        position_manager.update_position(&sol).unwrap();

        let positions = position_manager.get_positions(None).unwrap();
        assert_eq!(positions.len(), 3, "Should track 3 different assets");
    }

    #[test]
    fn test_position_margin_calculation() {
        let position_manager = PositionManager::new();

        // Open leveraged position
        let execution = create_test_execution("BTC-USD", 10.0, 50000.0); // $500k notional
        position_manager.update_position(&execution).unwrap();

        let positions = position_manager
            .get_positions(Some("BTC-USD".to_owned()))
            .unwrap();
        if let Some(position) = positions.first() {
            // Margin requirement should be calculated
            assert!(position.margin_requirement >= Decimal::ZERO);
        }
    }

    #[test]
    fn test_position_average_price_calculation() {
        let position_manager = PositionManager::new();

        // Multiple executions at different prices
        let exec1 = create_test_execution("BTC-USD", 1.0, 50000.0);
        let exec2 = create_test_execution("BTC-USD", 1.0, 51000.0);
        let exec3 = create_test_execution("BTC-USD", 1.0, 49000.0);

        position_manager.update_position(&exec1).unwrap();
        position_manager.update_position(&exec2).unwrap();
        position_manager.update_position(&exec3).unwrap();

        let positions = position_manager
            .get_positions(Some("BTC-USD".to_owned()))
            .unwrap();
        if let Some(position) = positions.first() {
            // Average should be ~50000
            let avg = position.avg_price.to_f64().unwrap();
            assert!(
                (49500.0..=50500.0).contains(&avg),
                "Average price should be ~50000, got {}",
                avg
            );
        }
    }

    #[test]
    fn test_position_state_consistency() {
        let position_manager = PositionManager::new();

        // Open position
        let open = create_test_execution("TEST-USD", 100.0, 10.0);
        position_manager.update_position(&open).unwrap();

        // Verify state
        let positions1 = position_manager
            .get_positions(Some("TEST-USD".to_owned()))
            .unwrap();
        assert!(!positions1.is_empty(), "TEST-USD position should exist");

        // Update position
        let update = create_test_execution("TEST-USD", 50.0, 11.0);
        position_manager.update_position(&update).unwrap();

        // State should be consistent
        let positions2 = position_manager
            .get_positions(Some("TEST-USD".to_owned()))
            .unwrap();
        assert!(
            !positions2.is_empty(),
            "TEST-USD position should still exist"
        );
    }
}

// ============================================================================
// Risk Integration Tests (8+ tests)
// ============================================================================

#[cfg(test)]
mod risk_integration_tests {
    use super::*;

    #[tokio::test]
    async fn test_order_validation_with_risk_checks() {
        let order_manager = OrderManager::new();

        // Valid order should pass risk check
        let valid_order = create_test_order("BTC-USD", OrderSide::Buy, 0.1, 50000.0);
        let result = order_manager.validate_order(&valid_order).await;
        assert!(result.is_ok(), "Valid order should pass risk checks");
    }

    #[tokio::test]
    async fn test_position_limit_enforcement() {
        let position_manager = PositionManager::new();

        // Large position
        let large_execution = create_test_execution("BTC-USD", 1000.0, 50000.0);
        let result = position_manager.update_position(&large_execution);

        // Should succeed (risk limits enforced by risk manager)
        result.unwrap();
    }

    #[tokio::test]
    async fn test_leverage_limit_validation() {
        let order_manager = OrderManager::new();

        // High leverage order
        let order = create_test_order("ETH-USD", OrderSide::Buy, 100.0, 3000.0);
        let result = order_manager.validate_order(&order).await;

        // Basic validation should pass (leverage checked by risk manager)
        result.unwrap();
    }

    #[tokio::test]
    async fn test_real_time_risk_limit_enforcement() {
        let engine = create_test_engine().await;

        // Submit order that may hit risk limits
        let result = engine
            .submit_order(
                "BTC-USD".to_owned(),
                OrderSide::Buy,
                OrderType::Market,
                Decimal::from_str("100.0").unwrap(),
                None,
                None,
            )
            .await;

        // Should succeed or fail with risk message
        assert!(result.is_ok() || result.unwrap_err().contains("risk"));
    }

    #[tokio::test]
    async fn test_concurrent_risk_checks() {
        let order_manager = Arc::new(OrderManager::new());
        let mut handles = vec![];

        // 50 concurrent risk checks
        for i in 0..50 {
            let manager_clone = Arc::clone(&order_manager);
            let handle = tokio::spawn(async move {
                let order = create_test_order(
                    "BTC-USD",
                    if i % 2 == 0 {
                        OrderSide::Buy
                    } else {
                        OrderSide::Sell
                    },
                    1.0,
                    50000.0,
                );
                manager_clone.validate_order(&order).await
            });
            handles.push(handle);
        }

        let results: Vec<_> = futures::future::join_all(handles).await;
        let success_count = results
            .iter()
            .filter(|r| r.is_ok() && r.as_ref().unwrap().is_ok())
            .count();

        assert!(success_count >= 45, "Most risk checks should succeed");
    }

    #[tokio::test]
    async fn test_risk_check_before_execution() {
        let engine = create_test_engine().await;

        // Order should be risk-checked before execution
        let result = engine
            .submit_order(
                "BTC-USD".to_owned(),
                OrderSide::Buy,
                OrderType::Limit,
                Decimal::from_str("1.0").unwrap(),
                Some(Decimal::from_str("50000.0").unwrap()),
                None,
            )
            .await;

        assert!(result.is_ok(), "Order with risk check should succeed");
    }

    #[tokio::test]
    async fn test_position_risk_tracking() {
        let position_manager = PositionManager::new();

        // Track position risk
        let execution = create_test_execution("BTC-USD", 5.0, 50000.0);
        position_manager.update_position(&execution).unwrap();

        let positions = position_manager.get_positions(None).unwrap();
        assert!(!positions.is_empty(), "Position risk should be tracked");
    }

    #[tokio::test]
    async fn test_risk_integration_with_multiple_positions() {
        let position_manager = PositionManager::new();

        // Multiple positions for risk aggregation
        let btc = create_test_execution("BTC-USD", 1.0, 50000.0);
        let eth = create_test_execution("ETH-USD", 10.0, 3000.0);
        let sol = create_test_execution("SOL-USD", 100.0, 100.0);

        position_manager.update_position(&btc).unwrap();
        position_manager.update_position(&eth).unwrap();
        position_manager.update_position(&sol).unwrap();

        let positions = position_manager.get_positions(None).unwrap();
        assert_eq!(
            positions.len(),
            3,
            "All positions should be tracked for risk"
        );
    }
}

// ============================================================================
// State Consistency Tests (5+ tests)
// ============================================================================

#[cfg(test)]
mod state_consistency_tests {
    use super::*;

    #[tokio::test]
    async fn test_order_state_machine_created_to_pending() {
        let order_manager = OrderManager::new();
        let order = create_test_order("BTC-USD", OrderSide::Buy, 1.0, 50000.0);
        let order_id = order.id;

        order_manager.add_order(order).await;

        let result = order_manager
            .update_order_status(&order_id, OrderStatus::Pending)
            .await;
        assert!(
            result.is_ok(),
            "Created -> Pending transition should succeed"
        );

        let updated_order = order_manager.get_order(&order_id).await.unwrap();
        assert_eq!(updated_order.status, OrderStatus::Pending);
    }

    #[tokio::test]
    async fn test_order_state_machine_pending_to_filled() {
        let order_manager = OrderManager::new();
        let mut order = create_test_order("ETH-USD", OrderSide::Sell, 10.0, 3000.0);
        order.status = OrderStatus::Pending;
        let order_id = order.id;

        order_manager.add_order(order).await;

        let result = order_manager
            .update_order_status(&order_id, OrderStatus::Filled)
            .await;
        assert!(
            result.is_ok(),
            "Pending -> Filled transition should succeed"
        );

        let updated_order = order_manager.get_order(&order_id).await.unwrap();
        assert_eq!(updated_order.status, OrderStatus::Filled);
    }

    #[tokio::test]
    async fn test_order_state_machine_pending_to_cancelled() {
        let order_manager = OrderManager::new();
        let mut order = create_test_order("SOL-USD", OrderSide::Buy, 100.0, 100.0);
        order.status = OrderStatus::Pending;
        let order_id = order.id;

        order_manager.add_order(order).await;

        let result = order_manager
            .update_order_status(&order_id, OrderStatus::Cancelled)
            .await;
        assert!(
            result.is_ok(),
            "Pending -> Cancelled transition should succeed"
        );
    }

    #[tokio::test]
    async fn test_position_state_updates() {
        let position_manager = PositionManager::new();

        // State 1: Open position
        let open = create_test_execution("BTC-USD", 1.0, 50000.0);
        position_manager.update_position(&open).unwrap();

        let state1 = position_manager
            .get_positions(Some("BTC-USD".to_owned()))
            .unwrap();
        assert!(!state1.is_empty(), "BTC-USD position should exist");

        // State 2: Update position
        let update = create_test_execution("BTC-USD", 0.5, 51000.0);
        position_manager.update_position(&update).unwrap();

        let state2 = position_manager
            .get_positions(Some("BTC-USD".to_owned()))
            .unwrap();
        assert!(!state2.is_empty(), "BTC-USD position should still exist");

        // State should be consistent
        if let Some(pos) = state2.first() {
            assert!(pos.quantity > Decimal::ZERO);
        }
    }

    #[tokio::test]
    async fn test_concurrent_state_consistency() {
        let order_manager = Arc::new(OrderManager::new());
        let order = create_test_order("BTC-USD", OrderSide::Buy, 1.0, 50000.0);
        let order_id = order.id;

        order_manager.add_order(order).await;

        let mut handles = vec![];

        // 10 threads trying to update order status
        for i in 0..10 {
            let manager_clone = Arc::clone(&order_manager);
            let oid = order_id;
            let handle = tokio::spawn(async move {
                manager_clone
                    .update_order_status(
                        &oid,
                        if i % 2 == 0 {
                            OrderStatus::Pending
                        } else {
                            OrderStatus::Filled
                        },
                    )
                    .await
            });
            handles.push(handle);
        }

        futures::future::join_all(handles).await;

        // Final state should be consistent
        let final_order = order_manager.get_order(&order_id).await;
        assert!(final_order.is_some(), "Order should still exist");
    }
}
