use std::path::Path;

/// Which VCS is active in a directory, detected by filesystem inspection.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VcsDetection {
    /// Both `.jj/` and `.git/` present — jj colocated mode (preferred for
    /// Radicle interop).
    JjColocated,
    /// Only `.jj/` present.
    Jj,
    /// Only `.git/` present.
    Git,
    /// Neither found.
    None,
}

/// Detect VCS by inspecting `path` for `.jj/` and `.git/` directories.
pub fn detect_vcs(path: &Path) -> VcsDetection {
    let has_jj = path.join(".jj").is_dir();
    let has_git = path.join(".git").is_dir();
    match (has_jj, has_git) {
        (true, true) => VcsDetection::JjColocated,
        (true, false) => VcsDetection::Jj,
        (false, true) => VcsDetection::Git,
        (false, false) => VcsDetection::None,
    }
}

#[cfg(test)]
mod tests {
    use tempfile::TempDir;

    use super::*;

    #[test]
    fn detect_none() {
        let dir = TempDir::new().unwrap();
        assert_eq!(detect_vcs(dir.path()), VcsDetection::None);
    }

    #[test]
    fn detect_git_only() {
        let dir = TempDir::new().unwrap();
        std::fs::create_dir(dir.path().join(".git")).unwrap();
        assert_eq!(detect_vcs(dir.path()), VcsDetection::Git);
    }

    #[test]
    fn detect_jj_only() {
        let dir = TempDir::new().unwrap();
        std::fs::create_dir(dir.path().join(".jj")).unwrap();
        assert_eq!(detect_vcs(dir.path()), VcsDetection::Jj);
    }

    #[test]
    fn detect_colocated() {
        let dir = TempDir::new().unwrap();
        std::fs::create_dir(dir.path().join(".jj")).unwrap();
        std::fs::create_dir(dir.path().join(".git")).unwrap();
        assert_eq!(detect_vcs(dir.path()), VcsDetection::JjColocated);
    }
}
