use crate::{config::Config, log::Ansi};
use anyhow::Result;
use riptide_amm::fetch_all_strategy_with_filter;

fn account_cell(addr: impl ToString) -> String {
    let full = addr.to_string();
    let short = if full.len() <= 8 {
        full.clone()
    } else {
        format!("{}...{}", &full[..4], &full[full.len() - 4..])
    };
    short.link(&format!("https://solscan.io/account/{full}"))
}

pub fn handle(config: &Config) -> Result<()> {
    let rpc = config.rpc_client();
    let markets = fetch_all_strategy_with_filter(&rpc, vec![])?;

    println!("| Market      | Authority   | Updater     | Mint A      | Mint B      |");
    println!("|-------------|-------------|-------------|-------------|-------------|");
    for m in &markets {
        println!(
            "| {} | {} | {} | {} | {} |",
            account_cell(m.address),
            account_cell(m.data.authority),
            account_cell(m.data.updater),
            account_cell(m.data.mint_a),
            account_cell(m.data.mint_b),
        );
    }

    Ok(())
}
