//! cTrader Open API configuration.

use serde::{Deserialize, Serialize};

/// Target environment (demo vs. live).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum CTraderEnvironment {
    /// `demo.ctraderapi.com:5035`
    Demo,
    /// `live.ctraderapi.com:5035`
    Live,
}

impl CTraderEnvironment {
    /// TCP host for the environment.
    pub const fn host(&self) -> &'static str {
        match self {
            Self::Demo => "demo.ctraderapi.com",
            Self::Live => "live.ctraderapi.com",
        }
    }

    /// TCP port (both envs use 5035).
    pub const fn port(&self) -> u16 {
        5035
    }
}

impl Default for CTraderEnvironment {
    fn default() -> Self {
        Self::Demo
    }
}

/// Configuration for a cTrader Open API connection.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CTraderConfig {
    /// OAuth2 application client ID.
    pub client_id: String,

    /// OAuth2 application client secret.
    pub client_secret: String,

    /// OAuth2 access token (obtained via auth code or refresh flow).
    pub access_token: String,

    /// cTID trader account ID.
    pub account_id: i64,

    /// Demo or Live environment.
    #[serde(default)]
    pub environment: CTraderEnvironment,

    /// Heartbeat interval in seconds (cTrader requires ≤ 10s).
    #[serde(default = "default_heartbeat_interval")]
    pub heartbeat_interval_secs: u64,

    /// Per-request timeout in milliseconds.
    #[serde(default = "default_request_timeout")]
    pub request_timeout_ms: u64,

    /// Maximum reconnection attempts before giving up.
    #[serde(default = "default_max_reconnect")]
    pub max_reconnect_attempts: u32,
}

const fn default_heartbeat_interval() -> u64 {
    10
}
const fn default_request_timeout() -> u64 {
    5000
}
const fn default_max_reconnect() -> u32 {
    5
}
