use napi_derive::napi;

pub mod market_data;
pub mod orders;
pub mod positions;
pub mod backtest;
pub mod strategies;
pub mod system;

// Main API entry point
#[napi]
pub struct TradingAPI {
    inner: std::sync::Arc<crate::TradingCore>,
}

#[napi]
impl TradingAPI {
    #[napi(constructor)]
    pub fn new(mode: String) -> napi::Result<Self> {
        let trading_mode = parse_mode(&mode)?;
        let core = crate::TradingCore::new(trading_mode)
            .map_err(|e| napi::Error::from_reason(e))?;
        
        Ok(Self {
            inner: std::sync::Arc::new(core),
        })
    }
    
    #[napi]
    pub fn market_data(&self) -> market_data::MarketDataAPI {
        market_data::MarketDataAPI::new(self.inner.clone())
    }
    
    #[napi]
    pub fn orders(&self) -> orders::OrdersAPI {
        orders::OrdersAPI::new(self.inner.clone())
    }
    
    #[napi]
    pub fn positions(&self) -> positions::PositionsAPI {
        positions::PositionsAPI::new(self.inner.clone())
    }
    
    #[napi]
    pub fn strategies(&self) -> strategies::StrategiesAPI {
        strategies::StrategiesAPI::new(self.inner.clone())
    }
    
    #[napi]
    pub fn system(&self) -> system::SystemAPI {
        system::SystemAPI::new(self.inner.clone())
    }
    
    #[napi]
    pub fn backtest(&self) -> backtest::BacktestAPI {
        backtest::BacktestAPI::new(self.inner.clone())
    }
}

fn parse_mode(mode: &str) -> napi::Result<crate::TradingMode> {
    match mode {
        "backtest" => Ok(crate::TradingMode::Backtest {
            start_time: chrono::Utc::now(),
            end_time: chrono::Utc::now(),
            speed_multiplier: 1.0,
        }),
        "paper" => Ok(crate::TradingMode::Paper {
            starting_capital: 100_000.0,
        }),
        "live" => Ok(crate::TradingMode::Live {
            broker: "default".to_string(),
            account_id: "default".to_string(),
        }),
        _ => Err(napi::Error::from_reason(format!("Unknown mode: {}", mode))),
    }
}