use bpx_api_types::borrow_lend::BorrowLendPosition;
use tabled::settings::Style;
use tabled::{Table, Tabled};

use crate::output::OutputFormat;

#[derive(Tabled)]
struct BorrowRow {
    #[tabled(rename = "ID")]
    id: String,
    #[tabled(rename = "SYMBOL")]
    symbol: String,
    #[tabled(rename = "NET QTY")]
    net_quantity: String,
    #[tabled(rename = "EXPOSURE QTY")]
    exposure_quantity: String,
    #[tabled(rename = "EXPOSURE NOTIONAL")]
    exposure_notional: String,
    #[tabled(rename = "MARK PRICE")]
    mark_price: String,
    #[tabled(rename = "INTEREST")]
    interest: String,
}

pub fn print_borrow_positions(positions: &[BorrowLendPosition], format: OutputFormat) {
    match format {
        OutputFormat::Json => super::print_json(positions),
        OutputFormat::Table => {
            if positions.is_empty() {
                println!("No borrow/lend positions found.");
                return;
            }
            let rows: Vec<BorrowRow> = positions
                .iter()
                .map(|p| BorrowRow {
                    id: p.id.clone(),
                    symbol: p.symbol.clone(),
                    net_quantity: p.net_quantity.to_string(),
                    exposure_quantity: p.net_exposure_quantity.to_string(),
                    exposure_notional: p.net_exposure_notional.to_string(),
                    mark_price: p.mark_price.to_string(),
                    interest: p.cumulative_interest.to_string(),
                })
                .collect();
            let mut table = Table::new(rows);
            table.with(Style::blank());
            println!("{table}");
        }
    }
}
