//! Language detection for projects.

use std::path::Path;

use crate::types::Language;

/// Detect the primary language of a project.
///
/// Priority:
/// 1. Explicit language in config (handled by caller)
/// 2. Presence of language-specific manifest files
/// 3. File count heuristic (most common extension)
///
/// Not yet wired into the scan pipeline; will be used when
/// auto-detection replaces the config-based language setting.
#[allow(dead_code)]
pub fn detect_project_language(root: &Path) -> Language {
    // Check for manifest files
    if root.join("Cargo.toml").exists() {
        return Language::Rust;
    }
    if root.join("go.mod").exists() {
        return Language::Go;
    }
    if root.join("package.json").exists() {
        // Could be TypeScript or JavaScript
        if root.join("tsconfig.json").exists() {
            return Language::TypeScript;
        }
        return Language::JavaScript;
    }
    if root.join("pyproject.toml").exists() || root.join("requirements.txt").exists() {
        return Language::Python;
    }

    // Fallback: Unknown
    Language::Unknown
}

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

    #[test]
    fn test_detect_rust_project() {
        let dir = TempDir::new().expect("create temp dir");
        std::fs::write(dir.path().join("Cargo.toml"), "[package]").expect("write file");

        assert_eq!(detect_project_language(dir.path()), Language::Rust);
    }

    #[test]
    fn test_detect_go_project() {
        let dir = TempDir::new().expect("create temp dir");
        std::fs::write(dir.path().join("go.mod"), "module test").expect("write file");

        assert_eq!(detect_project_language(dir.path()), Language::Go);
    }

    #[test]
    fn test_detect_typescript_project() {
        let dir = TempDir::new().expect("create temp dir");
        std::fs::write(dir.path().join("package.json"), "{}").expect("write file");
        std::fs::write(dir.path().join("tsconfig.json"), "{}").expect("write file");

        assert_eq!(detect_project_language(dir.path()), Language::TypeScript);
    }

    #[test]
    fn test_detect_unknown() {
        let dir = TempDir::new().expect("create temp dir");

        assert_eq!(detect_project_language(dir.path()), Language::Unknown);
    }
}
