//! Configuration management

use anyhow::{Result, Context};
use serde::Deserialize;

#[derive(Debug, Clone, Deserialize)]
pub struct Config {
    /// Host to bind to
    pub host: String,
    /// Port to listen on
    pub port: u16,
    /// Database path for vector store
    pub database_path: String,
    /// Default model provider (Qwen for QwenClaw)
    pub default_provider: String,
    /// Default model name
    pub default_model: String,
    /// API keys for providers
    pub qwen_api_key: Option<String>,
    pub openai_api_key: Option<String>,
    pub anthropic_api_key: Option<String>,
}

impl Config {
    pub fn from_env() -> Result<Self> {
        Ok(Self {
            host: std::env::var("RIG_HOST").unwrap_or_else(|_| "127.0.0.1".to_string()),
            port: std::env::var("RIG_PORT")
                .unwrap_or_else(|_| "8080".to_string())
                .parse()
                .context("Invalid RIG_PORT")?,
            database_path: std::env::var("RIG_DATABASE_PATH")
                .unwrap_or_else(|_| "rig-store.db".to_string()),
            // QwenClaw default: Qwen provider
            default_provider: std::env::var("RIG_DEFAULT_PROVIDER")
                .unwrap_or_else(|_| "qwen".to_string()),
            default_model: std::env::var("RIG_DEFAULT_MODEL")
                .unwrap_or_else(|_| "qwen-plus".to_string()),
            qwen_api_key: std::env::var("QWEN_API_KEY").ok(),
            openai_api_key: std::env::var("OPENAI_API_KEY").ok(),
            anthropic_api_key: std::env::var("ANTHROPIC_API_KEY").ok(),
        })
    }
}
