//! Broker configuration module
//! This module provides configuration structures for broker connections

use serde::{Deserialize, Serialize};

/// Interactive Brokers configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
/// InteractiveBrokersConfig
///
/// Auto-generated documentation placeholder - enhance with specifics
pub struct InteractiveBrokersConfig {
    /// Enabled
    pub enabled: bool,
    /// Account Id
    pub account_id: Option<String>,
    /// Host
    pub host: String,
    /// Port
    pub port: u16,
    /// Client Id
    pub client_id: i32,
}

impl Default for InteractiveBrokersConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            account_id: None,
            host: "127.0.0.1".to_owned(),
            port: 7497,
            client_id: 1,
        }
    }
}

/// `ICMarkets` configuration for cTrader OpenAPI integration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ICMarketsConfig {
    /// Whether the broker is enabled.
    pub enabled: bool,
    /// cTrader OAuth2 client ID.
    pub client_id: String,
    /// cTrader OAuth2 client secret.
    pub client_secret: String,
    /// cTrader OAuth2 access token.
    pub access_token: String,
    /// cTrader trader account ID (numeric).
    pub account_id: i64,
    /// Environment: "demo" or "live".
    pub environment: String,
    /// Heartbeat interval in seconds (default 10).
    #[serde(default = "default_heartbeat_secs")]
    pub heartbeat_interval_secs: u64,
    /// Request timeout in milliseconds (default 5000).
    #[serde(default = "default_request_timeout_ms")]
    pub request_timeout_ms: u64,
    /// Maximum reconnection attempts (default 5).
    #[serde(default = "default_max_reconnect")]
    pub max_reconnect_attempts: u32,
}

fn default_heartbeat_secs() -> u64 {
    10
}
fn default_request_timeout_ms() -> u64 {
    5000
}
fn default_max_reconnect() -> u32 {
    5
}

impl Default for ICMarketsConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            client_id: String::new(),
            client_secret: String::new(),
            access_token: String::new(),
            account_id: 0,
            environment: "demo".to_owned(),
            heartbeat_interval_secs: default_heartbeat_secs(),
            request_timeout_ms: default_request_timeout_ms(),
            max_reconnect_attempts: default_max_reconnect(),
        }
    }
}

/// Broker configurations
#[derive(Debug, Clone, Serialize, Deserialize)]
/// BrokerConfigs
///
/// Auto-generated documentation placeholder - enhance with specifics
pub struct BrokerConfigs {
    /// Interactive Brokers
    pub interactive_brokers: InteractiveBrokersConfig,
    /// Icmarkets
    pub icmarkets: ICMarketsConfig,
}

impl Default for BrokerConfigs {
    fn default() -> Self {
        Self {
            interactive_brokers: InteractiveBrokersConfig::default(),
            icmarkets: ICMarketsConfig::default(),
        }
    }
}

/// Routing configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
/// RoutingConfig
///
/// Auto-generated documentation placeholder - enhance with specifics
pub struct RoutingConfig {
    /// Default Broker
    pub default_broker: String,
    /// Rules
    pub rules: Vec<String>, // Placeholder for routing rules
}

impl Default for RoutingConfig {
    fn default() -> Self {
        Self {
            default_broker: "InteractiveBrokers".to_owned(),
            rules: vec![],
        }
    }
}

// NOTE: BrokerConnectorConfig exists in canonical config crate but not exported
// Keeping local definition until canonical export is available
/// Main broker connector configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
/// BrokerConnectorConfig
///
/// Auto-generated documentation placeholder - enhance with specifics
pub struct BrokerConnectorConfig {
    /// Brokers
    pub brokers: BrokerConfigs,
    /// Routing
    pub routing: RoutingConfig,
    /// Fail On Broker Error
    pub fail_on_broker_error: bool,
}

impl Default for BrokerConnectorConfig {
    fn default() -> Self {
        Self {
            brokers: BrokerConfigs::default(),
            routing: RoutingConfig::default(),
            fail_on_broker_error: false,
        }
    }
}
