//! Cursor local config reader.
//!
//! Cursor stores its MCP server list in `~/.cursor/mcp.json` as of the
//! 2026-04 release stream. The format is the same `mcpServers` map
//! Claude Desktop uses. We document the assumption here: if Cursor
//! shifts its format or path, this module is the single place to update
//! the path constant and the deserializer.
//!
//! Path: `~/.cursor/mcp.json` on every platform. Project-local
//! `.cursor/mcp.json` files (per repo) are out of scope for this
//! discovery walk; the runner picks those up via the explicit
//! `--config` flag's higher-priority discovery rules.
//!
//! Read-only.

use std::path::PathBuf;

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

/// Discover servers declared in `~/.cursor/mcp.json`.
///
/// Empty vec when the file does not exist (Cursor is not installed, or
/// the user keeps their MCP config project-local).
pub fn discover_servers() -> Result<Vec<DiscoveredServer>, DiscoveryError> {
    let Some(path) = config_path() else {
        return Ok(Vec::new());
    };
    parse_mcp_servers_file(&path, ClientSource::Cursor)
}

/// Resolve `~/.cursor/mcp.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(".cursor").join("mcp.json"))
}
