//! Anthropic reasoning example.
//!
//! Run:
//! ```sh
//! ANTHROPIC_API_KEY="your-key" cargo run -p querymt --example anthropic_thinking_example
//! ```
//!
//! Optional: set `PROVIDER_CONFIG` to a custom providers file path.

use querymt::{
    chat::{ChatMessage, ReasoningEffort},
    dynamic::PluginRegistryDynamicExt,
    plugin::host::PluginRegistry,
};

fn build_registry() -> Result<PluginRegistry, Box<dyn std::error::Error>> {
    let cfg_path =
        std::env::var("PROVIDER_CONFIG").unwrap_or_else(|_| "providers.toml".to_string());
    let registry =
        PluginRegistry::from_path(std::path::PathBuf::from(cfg_path))?.with_dynamic_loaders();
    Ok(registry)
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Get Anthropic API key from environment variable or use test key as fallback
    let api_key =
        std::env::var("ANTHROPIC_API_KEY").expect("Set ANTHROPIC_API_KEY to run this example");
    let registry = build_registry()?;

    // Initialize and configure the LLM client
    let llm = registry
        .builder("anthropic") // Use Anthropic (Claude) as the LLM provider
        .api_key(api_key) // Set the API key
        .model("claude-sonnet-4-6") // Use Claude Sonnet model
        .max_tokens(1500) // Limit response length
        .temperature(1.0) // Control response randomness (0.0-1.0)
        .reasoning_effort(ReasoningEffort::High)
        .reasoning_budget_tokens(1024)
        .build()
        .await?;

    // Prepare conversation history with a reasoning puzzle
    let messages = vec![
        ChatMessage::user()
            .text("How much r in strawberry?")
            .build(),
    ];

    // Send chat request and handle the response
    match llm.chat(&messages).await {
        Ok(text) => {
            if let Some(thinking) = text.thinking() {
                println!("Thinking: {}", thinking);
            }
            if let Some(text) = text.text() {
                println!("Text: {}", text);
            }
        }
        Err(e) => eprintln!("Chat error: {}", e),
    }

    Ok(())
}
