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

/// Markers checked in priority order when walking up from a target path.
const MARKERS: &[&str] = &["botignore.toml", ".botignore", ".git"];

/// Walk upward from `target` (or its parent if `target` is a file) looking
/// for the nearest project root. Roots are identified by the presence of
/// any marker in `MARKERS`. Walks from the **target file's location**, not
/// from cwd, because agents `cd` around.
pub fn find_project_root(target: &Path) -> Option<PathBuf> {
    let start = if target.is_file() {
        target.parent()?
    } else {
        target
    };

    let mut current = Some(start);
    while let Some(dir) = current {
        for marker in MARKERS {
            if dir.join(marker).exists() {
                return Some(dir.to_path_buf());
            }
        }
        current = dir.parent();
    }
    None
}
