use anyhow::{Context, Result};
use kreuzberg::ExtractionConfig;
use std::path::PathBuf;
pub fn load_config(config_path: Option<PathBuf>) -> Result<ExtractionConfig> {
if let Some(path) = config_path {
let path_str = path.to_string_lossy();
let path_lower = path_str.to_lowercase();
let config = if path_lower.ends_with(".toml") {
ExtractionConfig::from_toml_file(&path)
} else if path_lower.ends_with(".yaml") || path_lower.ends_with(".yml") {
ExtractionConfig::from_yaml_file(&path)
} else if path_lower.ends_with(".json") {
ExtractionConfig::from_json_file(&path)
} else {
anyhow::bail!("Config file must have .toml, .yaml, .yml, or .json extension (case-insensitive)");
};
config.with_context(|| format!("Failed to load configuration from '{}'. Ensure the file exists, is readable, and contains valid configuration.", path.display()))
} else {
match ExtractionConfig::discover() {
Ok(Some(config)) => Ok(config),
Ok(None) => Ok(ExtractionConfig::default()),
Err(e) => Err(e).context("Failed to auto-discover configuration file. Searched for kreuzberg.{toml,yaml,json} in current and parent directories. Use --config to specify an explicit path."),
}
}
}