use anyhow::{Context, Result, bail};
use rqmd_core::Store;
use rqmd_core::collections::{Config, ModelsConfig};
use rqmd_core::llm::config::resolve_models;
use rqmd_core::llm::types::ModelResolutionConfig;
use rqmd_core::paths::rqmd_homedir;
use rqmd_core::store::path::{pwd, real_path};
use rqmd_core::store::store_config::sync_config_to_db;
use crate::color::Palette;
pub fn run(p: &Palette) -> Result<()> {
let cwd = pwd();
if real_path(&cwd) == real_path(&rqmd_homedir()) {
bail!(
"Refusing to initialize a local index in $HOME. The global index is \
automatically created; run `rqmd collection add <path>` for the global \
index, or run `rqmd init` inside a project folder."
);
}
let rqmd_dir = cwd.join(".rqmd");
std::fs::create_dir_all(&rqmd_dir)
.with_context(|| format!("creating {}", rqmd_dir.display()))?;
let yaml = rqmd_dir.join("index.yaml");
let config_path = if yaml.is_file() {
yaml
} else {
rqmd_dir.join("index.yml")
};
let db_path = rqmd_dir.join("index.sqlite");
let mut cfg = Config::from_file(&config_path)
.with_context(|| format!("loading config at {}", config_path.display()))?;
let existing = ModelResolutionConfig {
embed: cfg.data().models.as_ref().and_then(|m| m.embed.clone()),
generate: cfg.data().models.as_ref().and_then(|m| m.generate.clone()),
rerank: cfg.data().models.as_ref().and_then(|m| m.rerank.clone()),
};
let resolved = resolve_models(Some(&existing));
cfg.set_models(ModelsConfig {
embed: Some(resolved.embed),
rerank: Some(resolved.rerank),
generate: Some(resolved.generate),
})
.with_context(|| format!("writing config at {}", config_path.display()))?;
let mut store =
Store::open(&db_path).with_context(|| format!("opening index at {}", db_path.display()))?;
store
.with_connection_mut(|conn| sync_config_to_db(conn, &cfg))
.context("syncing config into the new local index")?;
println!("Created local index at {}", rqmd_dir.display());
println!("{}ready to go with new local index{}", p.green(), p.reset());
Ok(())
}