use std::path::Path;
use std::sync::Arc;
use anyhow::{Context, Result};
use crate::config::{self, Config, DocumentsCliOverrides};
use crate::git::Repo;
use crate::git_cache::GitCache;
use crate::mcp::BasemindServer;
use crate::store::Store;
const CLI_GIT_CACHE_MEM: usize = 256;
pub fn build_server(
root: &Path,
view: &str,
documents: DocumentsCliOverrides,
) -> Result<BasemindServer> {
let store = Store::open_read_only(root, view).context("open store (read-only)")?;
let basemind_dir = root.join(config::BASEMIND_DIR);
let cfg = Arc::new(load_config(root, documents)?);
let repo = Repo::discover(root).ok().map(Arc::new);
let git_cache = Arc::new(
GitCache::open(&basemind_dir, CLI_GIT_CACHE_MEM, false).context("open git cache")?,
);
Ok(BasemindServer::new_oneshot(
store,
root.to_path_buf(),
cfg,
repo,
git_cache,
))
}
fn load_config(root: &Path, documents: DocumentsCliOverrides) -> Result<Config> {
match config::load_with_overrides(root, None, Some(documents)) {
Ok(loaded) => Ok(loaded.config),
Err(config::ConfigError::NotFound(_)) => Ok(config::default_for_root(root)),
Err(e) => Err(anyhow::anyhow!(e)),
}
}