use anyhow::Result;
use clap::Subcommand;

use crate::client;
use crate::config::ResolvedConfig;
use crate::output::borrow as out;
use crate::output::OutputFormat;

#[derive(Debug, Subcommand)]
pub enum BorrowCommand {
    /// List borrow/lend positions
    Positions,
}

pub async fn run(
    cmd: &BorrowCommand,
    config: &ResolvedConfig,
    secret: Option<&str>,
    base_url: Option<&str>,
    format: OutputFormat,
) -> Result<()> {
    let c = client::authenticated_client(config, secret, base_url)?;

    match cmd {
        BorrowCommand::Positions => {
            let sp = client::spinner_for("Fetching borrow/lend positions...", format);
            let result = c.get_borrow_lend_positions().await;
            sp.finish_and_clear();
            let positions = result.map_err(|e| anyhow::anyhow!("{e}"))?;
            out::print_borrow_positions(&positions, format);
        }
    }

    Ok(())
}
