//! Claude Code local config reader.
//!
//! Claude Code stores its MCP server list in `~/.claude/config.json`
//! (same `mcpServers` shape Claude Desktop uses). Reading it lets a
//! developer who only uses the CLI surface their servers to `mcptest`
//! without copying the JSON over.
//!
//! Path: `~/.claude/config.json` on every platform. We resolve `HOME`
//! ourselves (rather than calling `etcetera::home_dir`) so tests can swap
//! the directory by setting the env var.
//!
//! Read-only.

use std::path::PathBuf;

use super::{parse_mcp_servers_file, ClientSource, DiscoveredServer, DiscoveryError};

/// Discover servers declared in Claude Code's `~/.claude/config.json`.
///
/// Empty vec when the file does not exist (Claude Code is not
/// installed, or the user has not configured any servers). Errors
/// surface only when the file exists but fails to read or parse.
pub fn discover_servers() -> Result<Vec<DiscoveredServer>, DiscoveryError> {
    let Some(path) = config_path() else {
        return Ok(Vec::new());
    };
    parse_mcp_servers_file(&path, ClientSource::ClaudeCode)
}

/// Resolve `~/.claude/config.json`, or `None` when `$HOME` is unset.
pub(super) fn config_path() -> Option<PathBuf> {
    let home = std::env::var_os("HOME")?;
    Some(PathBuf::from(home).join(".claude").join("config.json"))
}
