use serde::Deserialize;

use crate::error::ApiError;
use super::InstrumentPair;

#[derive(Deserialize)]
struct Product {
    base_currency: String,
    quote_currency: String,
    status: String,
}

pub async fn fetch(client: &reqwest::Client) -> Result<Vec<InstrumentPair>, ApiError> {
    let products: Vec<Product> = client
        .get("https://api.exchange.coinbase.com/products")
        .send()
        .await
        .map_err(|e| ApiError::Internal(format!("coinbase request failed: {e}")))?
        .json()
        .await
        .map_err(|e| ApiError::Internal(format!("coinbase json parse failed: {e}")))?;

    let pairs = products
        .into_iter()
        .filter(|p| p.status == "online")
        .map(|p| InstrumentPair {
            base: p.base_currency,
            quote: p.quote_currency,
        })
        .collect();

    Ok(pairs)
}
