use std::path::{Path, PathBuf};

pub fn resolve_path(path: &str, allowed_dir: Option<&str>) -> Result<PathBuf, String> {
    let p = Path::new(path);
    let resolved = if p.is_absolute() {
        p.to_path_buf()
    } else {
        std::env::current_dir()
            .map_err(|e| format!("Failed to get current directory: {}", e))?
            .join(p)
    };

    if let Some(allowed) = allowed_dir {
        let allowed_path = Path::new(allowed);
        if !resolved.starts_with(allowed_path) {
            return Err(format!(
                "Path '{}' is outside allowed directory '{}'",
                path, allowed
            ));
        }
    }

    Ok(resolved)
}
