//! Tiny extension → MIME map for the small set of file types Relicario
//! attaches today. Unknown extensions fall back to `application/octet-stream`.

/// Guess a MIME type from a filename's extension. Case-insensitive.
pub fn guess_for_extension(filename: &str) -> &'static str {
    let lower = filename.to_ascii_lowercase();
    match lower.rsplit_once('.').map(|(_, ext)| ext).unwrap_or("") {
        "pdf" => "application/pdf",
        "png" => "image/png",
        "jpg" | "jpeg" => "image/jpeg",
        "txt" => "text/plain",
        "json" => "application/json",
        _ => "application/octet-stream",
    }
}

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

    #[test]
    fn known_extensions_match() {
        assert_eq!(guess_for_extension("doc.pdf"), "application/pdf");
        assert_eq!(guess_for_extension("photo.png"), "image/png");
        assert_eq!(guess_for_extension("photo.jpg"), "image/jpeg");
        assert_eq!(guess_for_extension("photo.jpeg"), "image/jpeg");
        assert_eq!(guess_for_extension("notes.txt"), "text/plain");
        assert_eq!(guess_for_extension("data.json"), "application/json");
    }

    #[test]
    fn extension_match_is_case_insensitive() {
        assert_eq!(guess_for_extension("doc.PDF"), "application/pdf");
        assert_eq!(guess_for_extension("photo.JPEG"), "image/jpeg");
    }

    #[test]
    fn unknown_or_missing_extension_falls_back() {
        assert_eq!(guess_for_extension("unknown.xyz"), "application/octet-stream");
        assert_eq!(guess_for_extension("noextension"), "application/octet-stream");
        assert_eq!(guess_for_extension(""), "application/octet-stream");
    }

    #[test]
    fn uses_extension_after_last_dot() {
        assert_eq!(guess_for_extension("path/to/file.pdf"), "application/pdf");
        assert_eq!(guess_for_extension("archive.tar.gz"), "application/octet-stream");
    }
}
