//! Test helper utilities for broker integration tests.
//!
//! Provides configurable test fixtures and utilities that respect environment
//! variables while allowing tests to specify their own configurations.

#![allow(
    clippy::absurd_extreme_comparisons,
    clippy::assertions_on_constants,
    clippy::assertions_on_result_states,
    clippy::clone_on_copy,
    clippy::decimal_literal_representation,
    clippy::doc_lazy_continuation,
    clippy::doc_markdown,
    clippy::double_comparisons,
    clippy::else_if_without_else,
    clippy::empty_drop,
    clippy::expect_used,
    clippy::field_reassign_with_default,
    clippy::format_push_string,
    clippy::if_then_some_else_none,
    clippy::indexing_slicing,
    clippy::integer_division,
    clippy::len_zero,
    clippy::let_and_return,
    clippy::let_underscore_must_use,
    clippy::manual_let_else,
    clippy::manual_range_contains,
    clippy::map_err_ignore,
    clippy::missing_const_for_fn,
    clippy::modulo_arithmetic,
    clippy::needless_range_loop,
    clippy::new_without_default,
    clippy::non_ascii_literal,
    clippy::nonminimal_bool,
    clippy::octal_escapes,
    clippy::overly_complex_bool_expr,
    clippy::redundant_clone,
    clippy::shadow_reuse,
    clippy::shadow_unrelated,
    clippy::similar_names,
    clippy::single_component_path_imports,
    clippy::single_match_else,
    clippy::str_to_string,
    clippy::string_slice,
    clippy::tests_outside_test_module,
    clippy::unnecessary_cast,
    clippy::unnecessary_get_then_check,
    clippy::unnecessary_unwrap,
    clippy::unseparated_literal_suffix,
    clippy::unwrap_or_default,
    clippy::unwrap_used,
    clippy::use_debug,
    clippy::useless_vec,
    clippy::wildcard_enum_match_arm,
    dead_code,
    deprecated,
    unreachable_pub,
    unused_assignments,
    unused_comparisons,
    unused_imports,
    unused_variables
)]

use data::brokers::interactive_brokers::IBConfig;

/// Creates an IBConfig with test-friendly defaults that can be overridden by environment.
///
/// This helper ensures tests work regardless of environment variable settings
/// by providing sensible defaults for testing while still respecting env vars
/// when explicitly set.
///
/// # Default Test Values
/// - Host: "127.0.0.1" (or IB_GATEWAY_HOST env var)
///
/// - Port: 7497 (paper trading, or IB_GATEWAY_PORT env var)
/// - Client ID: 1 (or IB_CLIENT_ID env var)
///
/// - Account ID: "DU123456" (or IB_ACCOUNT_ID env var)
pub fn test_ib_config() -> IBConfig {
    IBConfig::default()
}

/// Creates an IBConfig for paper trading with explicit values.
///
/// This bypasses environment variables and uses fixed test values.
pub fn test_ib_config_paper() -> IBConfig {
    IBConfig {
        host: std::env::var("IB_GATEWAY_HOST").unwrap_or_else(|_| "127.0.0.1".to_string()),
        port: std::env::var("IB_GATEWAY_PORT")
            .ok()
            .and_then(|s| s.parse().ok())
            .unwrap_or(7497),
        client_id: std::env::var("IB_CLIENT_ID")
            .ok()
            .and_then(|s| s.parse().ok())
            .unwrap_or(1),
        account_id: std::env::var("IB_ACCOUNT_ID").unwrap_or_else(|_| "DU123456".to_string()),
        connection_timeout: 30,
        heartbeat_interval: 30,
        max_reconnect_attempts: 5,
        request_timeout: 10,
    }
}

/// Creates an IBConfig for live trading with explicit values.
///
/// This bypasses environment variables and uses fixed test values.
pub fn test_ib_config_live() -> IBConfig {
    IBConfig {
        host: std::env::var("IB_GATEWAY_HOST").unwrap_or_else(|_| "127.0.0.1".to_string()),
        port: 7496, // Live trading port (hardcoded for test)
        client_id: std::env::var("IB_CLIENT_ID")
            .ok()
            .and_then(|s| s.parse().ok())
            .unwrap_or(1),
        account_id: "U123456".to_string(), // Live account format
        connection_timeout: 30,
        heartbeat_interval: 30,
        max_reconnect_attempts: 5,
        request_timeout: 10,
    }
}

/// Creates an IBConfig for IB Gateway with explicit values.
pub fn test_ib_config_gateway() -> IBConfig {
    IBConfig {
        host: std::env::var("IB_GATEWAY_HOST").unwrap_or_else(|_| "127.0.0.1".to_string()),
        port: 4001, // IB Gateway port (hardcoded for test)
        client_id: std::env::var("IB_CLIENT_ID")
            .ok()
            .and_then(|s| s.parse().ok())
            .unwrap_or(1),
        account_id: std::env::var("IB_ACCOUNT_ID").unwrap_or_else(|_| "DU123456".to_string()),
        connection_timeout: 30,
        heartbeat_interval: 30,
        max_reconnect_attempts: 5,
        request_timeout: 10,
    }
}

/// Gets the expected host from environment or default.
pub fn expected_host() -> String {
    std::env::var("IB_GATEWAY_HOST").unwrap_or_else(|_| "127.0.0.1".to_string())
}

/// Gets the expected port from environment or default (paper trading).
pub fn expected_port() -> u16 {
    std::env::var("IB_GATEWAY_PORT")
        .ok()
        .and_then(|s| s.parse().ok())
        .unwrap_or(7497)
}

/// Gets the expected client ID from environment or default.
pub fn expected_client_id() -> i32 {
    std::env::var("IB_CLIENT_ID")
        .ok()
        .and_then(|s| s.parse().ok())
        .unwrap_or(1)
}

/// Gets the expected account ID from environment or default.
pub fn expected_account_id() -> String {
    std::env::var("IB_ACCOUNT_ID").unwrap_or_else(|_| "DU123456".to_string())
}

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

    #[test]
    fn test_helper_configs_are_valid() {
        let paper = test_ib_config_paper();
        assert!(!paper.host.is_empty());
        assert!(paper.port > 0);

        let live = test_ib_config_live();
        assert_eq!(live.port, 7496);
        assert!(live.account_id.starts_with("U"));

        let gateway = test_ib_config_gateway();
        assert_eq!(gateway.port, 4001);
    }

    #[test]
    fn test_expected_values() {
        let host = expected_host();
        assert!(!host.is_empty());

        let port = expected_port();
        assert!(port > 0);

        let client_id = expected_client_id();
        assert!(client_id >= 0 && client_id <= 32767);
    }
}
