//! Windsurf local config reader.
//!
//! Windsurf (the Codeium editor) stores its MCP server list in
//! `~/.codeium/windsurf/mcp_config.json` on every platform, using the same
//! `mcpServers` map Claude Desktop and Cursor use. If Windsurf shifts its path
//! or format, this module is the single place to update. Read-only.

use std::path::PathBuf;

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

/// Discover servers declared in `~/.codeium/windsurf/mcp_config.json`.
///
/// Empty vec when the file does not exist (Windsurf is not installed).
pub fn discover_servers() -> Result<Vec<DiscoveredServer>, DiscoveryError> {
    let Some(path) = config_path() else {
        return Ok(Vec::new());
    };
    parse_mcp_servers_file(&path, ClientSource::Windsurf)
}

/// Resolve `~/.codeium/windsurf/mcp_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(".codeium")
            .join("windsurf")
            .join("mcp_config.json"),
    )
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn config_path_lands_under_codeium_windsurf() {
        if std::env::var_os("HOME").is_none() {
            return; // no home in this sandbox; the resolver returns None by design
        }
        let path = config_path().expect("HOME is set");
        assert!(
            path.ends_with(".codeium/windsurf/mcp_config.json"),
            "{path:?}"
        );
    }

    #[test]
    fn discover_is_empty_when_the_file_is_absent() {
        // HOME points at a real dir in CI; the windsurf file almost never
        // exists there, so the walk is a clean empty rather than an error.
        assert!(discover_servers().is_ok());
    }
}
