//! Language detection for Markdown code fences.

use std::path::Path;

/// Detects a Markdown code-fence language tag from a filename and file contents.
///
/// Extension-based detection is tried first. If the extension is unknown, the
/// first line is inspected for a shebang.
///
/// Returns an empty string when no language is detected.
pub fn detect_language(filename: &Path, contents: &str) -> &'static str {
    detect_from_extension(filename)
        .or_else(|| detect_from_shebang(contents))
        .unwrap_or("")
}

/// Detects a language tag from the file extension.
fn detect_from_extension(filename: &Path) -> Option<&'static str> {
    let ext = filename
        .extension()
        .and_then(|e| e.to_str())
        .map(|e| e.to_ascii_lowercase());
    let ext_str = ext.as_deref();
    match ext_str {
        Some("rs") => Some("rust"),
        Some("py") => Some("python"),
        Some("json") => Some("json"),
        Some("toml") => Some("toml"),
        Some("yaml") | Some("yml") => Some("yaml"),
        Some("js") | Some("jsx") => Some("javascript"),
        Some("ts") | Some("tsx") => Some("typescript"),
        _ => None,
    }
}

/// Detects a language tag from a shebang on the first line.
fn detect_from_shebang(contents: &str) -> Option<&'static str> {
    if let Some(first_line) = contents.lines().next()
        && first_line.starts_with("#!")
    {
        if first_line.contains("python") {
            return Some("python");
        }
        if first_line.contains("bash") || first_line.contains("/sh") || first_line.contains(" sh") {
            return Some("bash");
        }
    }
    None
}

#[cfg(test)]
mod tests {
    use std::path::Path;

    use super::detect_language;

    #[test]
    fn non_shebang_line_does_not_trigger_detection() {
        let contents = "this mentions bash but is not a shebang";
        assert_eq!(detect_language(Path::new("file"), contents), "");
    }

    #[test]
    fn detects_python_from_shebang() {
        let contents = "#!/usr/bin/python3\nprint('hi')";
        assert_eq!(detect_language(Path::new("file"), contents), "python");
    }

    #[test]
    fn sh_shebang_maps_to_bash_for_llm() {
        let contents = "#!/bin/sh\necho hi";
        assert_eq!(detect_language(Path::new("file"), contents), "bash");
    }

    #[test]
    fn env_shebang_is_detected() {
        let contents = "#!/usr/bin/env sh\necho hi";
        assert_eq!(detect_language(Path::new("file"), contents), "bash");
    }

    #[test]
    fn extension_overrides_shebang() {
        let contents = "#!/bin/bash\nprint('hello')";
        assert_eq!(detect_language(Path::new("main.py"), contents), "python");
    }

    #[test]
    fn javascript_family_extensions_map_correctly() {
        let cases = [
            ("file.js", "javascript"),
            ("file.jsx", "javascript"),
            ("file.ts", "typescript"),
            ("file.tsx", "typescript"),
        ];
        for (filename, expected) in cases {
            assert_eq!(detect_language(Path::new(filename), ""), expected);
        }
    }

    #[test]
    fn json_derived_extensions_map_correctly() {
        let cases = [
            ("file.json", "json"),
            ("file.yml", "yaml"),
            ("file.yaml", "yaml"),
        ];
        for (filename, expected) in cases {
            assert_eq!(detect_language(Path::new(filename), ""), expected);
        }
    }

    #[test]
    fn toml_extension_is_detected() {
        assert_eq!(detect_language(Path::new("Cargo.toml"), ""), "toml");
    }
}
