pub mod account;
pub mod basis;
pub mod borrow;
pub mod capital;
pub mod fills;
pub mod markets;
pub mod order;
pub mod portfolio;
pub mod positions;
pub mod rfq;
pub mod risk;
pub mod scenario;
pub mod strategy;
pub mod watch;

use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
use serde::Serialize;

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
pub enum OutputFormat {
    #[default]
    Table,
    Json,
}

pub fn print_json<T: Serialize + ?Sized>(value: &T) {
    match serde_json::to_string_pretty(value) {
        Ok(json) => println!("{json}"),
        Err(e) => eprintln!("Failed to serialize JSON: {e}"),
    }
}

/// Format a millisecond unix timestamp as human-readable UTC
pub fn format_ts_ms(ts: i64) -> String {
    DateTime::from_timestamp_millis(ts)
        .map(|dt: DateTime<Utc>| dt.format("%Y-%m-%d %H:%M:%S").to_string())
        .unwrap_or_else(|| ts.to_string())
}

/// Format a second unix timestamp as human-readable UTC
pub fn format_ts_secs(ts: u64) -> String {
    DateTime::from_timestamp(ts as i64, 0)
        .map(|dt: DateTime<Utc>| dt.format("%Y-%m-%d %H:%M:%S").to_string())
        .unwrap_or_else(|| ts.to_string())
}

/// Format a fee rate as percentage (0.001 -> "0.10%")
pub fn format_pct(d: &Decimal) -> String {
    let pct = d * Decimal::from(100);
    format!("{:.2}%", pct)
}

/// Format boolean as Yes/No
pub fn format_bool(b: bool) -> &'static str {
    if b {
        "Yes"
    } else {
        "No"
    }
}

/// Display helper for Side enum (BUY/SELL, no color)
pub fn display_side(side: &bpx_api_types::order::Side) -> &'static str {
    match side {
        bpx_api_types::order::Side::Bid => "BUY",
        bpx_api_types::order::Side::Ask => "SELL",
    }
}

/// Display helper for TimeInForce enum
pub fn display_tif(tif: &bpx_api_types::order::TimeInForce) -> &'static str {
    match tif {
        bpx_api_types::order::TimeInForce::GTC => "GTC",
        bpx_api_types::order::TimeInForce::IOC => "IOC",
        bpx_api_types::order::TimeInForce::FOK => "FOK",
    }
}
