use crate::models::registry::ModelRegistry;
use anyhow::Result;
use tracing::info;

const MODELS_DEV_URL: &str = "https://models.dev/api.json";

pub async fn fetch_registry() -> Result<ModelRegistry> {
    info!("Fetching model registry from {}", MODELS_DEV_URL);

    let client = reqwest::Client::builder()
        .timeout(std::time::Duration::from_secs(10))
        .build()?;

    let response = client.get(MODELS_DEV_URL).send().await?;

    if !response.status().is_success() {
        return Err(anyhow::anyhow!("Failed to fetch registry: HTTP {}", response.status()));
    }

    let registry: ModelRegistry = response.json().await?;
    info!("Successfully loaded model registry");

    Ok(registry)
}
