use std::collections::HashSet;
use std::path::Path;
use ignore::WalkBuilder;

use crate::language::detect_language;

/// Scan the project directory for files and return a deduplicated Vec of
/// language ids (e.g. ["rust", "python"]). This is intentionally
/// conservative: it caps at 5000 files and depth 8 to keep startup cost low.
pub fn detect_project_languages(project_dir: &Path) -> Vec<String> {
    let mut set = HashSet::new();
    let mut count = 0usize;

    let walker = WalkBuilder::new(project_dir)
        .standard_filters(true)
        .max_depth(Some(8))
        .build();

    for entry in walker.flatten() {
        if !entry.file_type().map(|t| t.is_file()).unwrap_or(false) {
            continue;
        }
        count += 1;
        if count > 5000 {
            break;
        }
        let path = entry.path();
        if let Some(lang) = detect_language(path) {
            set.insert(lang.as_str().to_string());
        }
    }

    let mut v: Vec<String> = set.into_iter().collect();
    v.sort();
    v
}
