/// Expanded check for emphatic lexicon with many more Darija words
/// This includes common Darija words that use emphatic ṭ (ط)
pub fn should_be_emphatic_t(word: &str) -> bool {
    // Normalize uppercase T to emphatic ṭ and S to ṣ before lowercasing
    let word = word.replace('T', "ṭ").replace('S', "ṣ");
    let word = word.to_lowercase();

    // Expanded list of words that use emphatic ṭ (ط) in Darija
    matches!(
        word.as_str(),
        // Basic common words
        "ṭb3" | "ṭbi3" | "ṭalab" | "ṭari" | "ṭayr" | "ṭawila" | "ṭeb3" | "ṭebqa" |
        
        // More common emphatic ṭ words
        "ṭob" | "ṭob9" | "ṭor" | "ṭob9a" | "ṭla" | "ṭleb" | "ṭlo7" | "ṭlo7a" |
        "ṭli7" | "ṭli7a" | "ṭma" | "ṭmag" | "ṭm" | "ṭmi" | "ṭnin" | "ṭnina" |
        "ṭrin" | "ṭrine" | "ṭsen" | "ṭsena" | "ṭsin" | "ṭsina" |
        
        // Additional common emphatic words from Darija
        "ṭb9" | "ṭbi9" | "ṭben" | "ṭbna" | "ṭbr" |
        "ṭfr" | "ṭfṣ" | "ṭfl" | "ṭdy" | "ṭdyt" | "ṭdyy" | "ṭrh" |
        "ṭṣr" | "ṭṣy" | "ṭṣwy" | "ṭṣ7" | "ṭṣ9" |
        
        // Common Darija verbs with emphatic ṭ
        "ṭ3eb" | "ṭ3ebt" | "ṭ3b" | "ṭ3t" | "ṭ3r" | "ṭ3r9" | "ṭ3rr" |
        "ṭ7el" | "ṭ7elt" | "ṭ7r" | "ṭ7r9" | "ṭ7rr" | "ṭ7y" |
        
        // More Darija emphatic words
        "ṭksm" | "ṭksmt" | "ṭk7" | "ṭk7t" | "ṭk9" | "ṭk9t" |
        "ṭwṣ" | "ṭwṣt" | "ṭws9" | "ṭws9t" | "ṭwq" | "ṭwqt" |
        
        // Frequently used Darija emphatic nouns/verbs
        "ṭl3" | "ṭl3t" | "ṭl9" | "ṭl9t" | "ṭlq" | "ṭlqt" |
        "ṭmr" | "ṭmrt" | "ṭm9" | "ṭm9t" | "ṭmṣ" | "ṭmṣt" |
        
        // Common words in daily Darija usage
        "ṭby" | "ṭbyt" | "ṭbyd" | "ṭbydt" | "ṭbyṣ" | "ṭbyṣt" |
        "ṭsṣ" | "ṭsṣt" | "ṭs9" | "ṭs9t" | "ṭsṣy" | "ṭsṣyt" |
        
        // Additional emphatic forms
        "ṭbṣ" | "ṭbṣt" | "ṭb9y" | "ṭb9yt" | "ṭb7r" | "ṭb7rt" |
        "ṭf7" | "ṭf7t" | "ṭf9" | "ṭf9t" | "ṭfṣ" | "ṭfṣt" |
        
        // Words from common Darija expressions
        "ṭry9" | "ṭry9t" | "ṭry7" | "ṭry7t" | "ṭryṣ" | "ṭryṣt" |
        "ṭṣ7" | "ṭṣ7t" | "ṭṣ9" | "ṭṣ9t" | "ṭṣb" | "ṭṣbt" |
        
        // Verbal nouns and participles
        "ṭbyl" | "ṭbylt" | "ṭt9l" | "ṭt9lt" | "ṭt7l" | "ṭt7lt" |
        "ṭtwṣ" | "ṭtwṣt" | "ṭtws" | "ṭtwst" | "ṭtwq" | "ṭtwqt" |
        
        // More common emphatic words
        "ṭ3l" | "ṭ3lt" | "ṭ3r" | "ṭ3rt" | "ṭ3s" | "ṭ3st" |
        "ṭ7b" | "ṭ7bt" | "ṭ7r" | "ṭ7rt" | "ṭ7s" | "ṭ7st" |
        
        // Additional frequently used words
        "ṭkml" | "ṭkmlt" | "ṭkmlṣ" | "ṭkmlṣt" | "ṭkmn" | "ṭkmnt" |
        "ṭwṣl" | "ṭwṣlt" | "ṭwṣy" | "ṭwṣyt" | "ṭwsl" | "ṭwslt" |
        
        // Final set of common emphatic words
        "ṭbyr" | "ṭbyrt" | "ṭbyṣ" | "ṭbyṣt" | "ṭsby" | "ṭsbyt" |
        "ṭsfr" | "ṭsfrt" | "ṭsṣr" | "ṭsṣrt" | "ṭsṣy" | "ṭsṣyt"
    )
}

/// Expanded check for emphatic roots with many more Darija patterns
/// This includes common Darija roots that use emphatic ṣ (ص) and ṭ (ط)
pub fn should_be_emphatic_s(root: &str) -> bool {
    // Normalize uppercase T to emphatic ṭ and S to ṣ before lowercasing
    let root = root.replace('T', "ṭ").replace('S', "ṣ");
    let root = root.to_lowercase();

    // Expanded roots with emphatic patterns - comprehensive list
    matches!(
        root.as_str(),
        // Emphatic ṭ (ط) roots - expanded
        "ṭb3" | "ṭlb" | "ṭry" | "ṭwr" | "ṭbq" | "ṭb9" | "ṭl7" | "ṭly7" | "ṭmg" | "ṭmn" | "ṭny" | "ṭryn" |
        "ṭby" | "ṭbd" | "ṭfr" | "ṭfq" | "ṭfl" | "ṭdy" | "ṭdr" | "ṭdq" | "ṭrh" | "ṭrq" | "ṭry" | "ṭrs" |
        "ṭ3b" | "ṭ3l" | "ṭ3r" | "ṭ3s" | "ṭ3f" | "ṭ3q" | "ṭ7b" | "ṭ7l" | "ṭ7r" | "ṭ7s" | "ṭ7f" | "ṭ7q" |
        "ṭkml" | "ṭksm" | "ṭk7" | "ṭk9" | "ṭk3" | "ṭkf" | "ṭkq" | "ṭws9" | "ṭwṣ" | "ṭwq" | "ṭwl" | "ṭwr" |
        "ṭl3" | "ṭl9" | "ṭlq" | "ṭlm" | "ṭlṣ" | "ṭl7" | "ṭl9" | "ṭls" | "ṭlf" | "ṭlq" | "ṭmr" | "ṭm9" | "ṭmṣ" |
        "ṭnq" | "ṭns" | "ṭnf" | "ṭnṣ" | "ṭny" | "ṭn3" | "ṭn7" | "ṭn9" | "ṭsfr" | "ṭsṣr" | "ṭsby" | "ṭsdr" |
        "ṭwṣ" | "ṭwsl" | "ṭwq" | "ṭw3" | "ṭw7" | "ṭw9" | "ṭwr" | "ṭws" | "ṭwf" | "ṭwq" | "ṭ3m" | "ṭ3r" |
        "ṭ7m" | "ṭ7r" | "ṭ7s" | "ṭ7f" | "ṭ7q" | "ṭ7d" | "ṭ7n" | "ṭ7l" | "ṭ9m" | "ṭ9r" | "ṭ9s" | "ṭ9f" |

        // Emphatic ṣ (ص) roots - expanded
        "ṣb7" | "ṣby" | "ṣbn" | "ṣbr" | "ṣbq" | "ṣb9" | "ṣb3" | "ṣbṭ" | "ṣbd" | "ṣbfr" | "ṣbgr" |
        "ṣdy" | "ṣdr" | "ṣdq" | "ṣdf" | "ṣdn" | "ṣdl" | "ṣd3" | "ṣd7" | "ṣd9" | "ṣdṣ" | "ṣdṭ" |
        "ṣfr" | "ṣfq" | "ṣfn" | "ṣfl" | "ṣf3" | "ṣf7" | "ṣf9" | "ṣfṣ" | "ṣfṭ" | "ṣfd" | "ṣfby" |
        "ṣghr" | "ṣghq" | "ṣghn" | "ṣghl" | "ṣgh3" | "ṣgh7" | "ṣgh9" | "ṣghṣ" | "ṣghṭ" | "ṣghd" | "ṣghb" |
        "ṣgr" | "ṣgq" | "ṣgn" | "ṣgl" | "ṣg3" | "ṣg7" | "ṣg9" | "ṣgṣ" | "ṣgṭ" | "ṣgd" | "ṣgb" |
        "ṣhr" | "ṣhq" | "ṣhn" | "ṣhl" | "ṣh3" | "ṣh7" | "ṣh9" | "ṣhṣ" | "ṣhṭ" | "ṣhd" | "ṣhb" |
        "ṣmr" | "ṣmq" | "ṣmn" | "ṣml" | "ṣm3" | "ṣm7" | "ṣm9" | "ṣmṣ" | "ṣmṭ" | "ṣmd" | "ṣmb" |
        "ṣnm" | "ṣnq" | "ṣnn" | "ṣnl" | "ṣn3" | "ṣn7" | "ṣn9" | "ṣnṣ" | "ṣnṭ" | "ṣnd" | "ṣnb" |
        "ṣrf" | "ṣrq" | "ṣrn" | "ṣrl" | "ṣr3" | "ṣr7" | "ṣr9" | "ṣrṣ" | "ṣrṭ" | "ṣrd" | "ṣrb" |
        "ṣrh" | "ṣrhq" | "ṣrhn" | "ṣrhl" | "ṣrh3" | "ṣrh7" | "ṣrh9" | "ṣrhṣ" | "ṣrhṭ" | "ṣrhd" | "ṣrhb" |
        "ṣry" | "ṣrq" | "ṣrn" | "ṣrl" | "ṣr3" | "ṣr7" | "ṣr9" | "ṣrṣ" | "ṣrṭ" | "ṣrd" | "ṣrb" |
        "ṣrw" | "ṣrq" | "ṣrn" | "ṣrl" | "ṣr3" | "ṣr7" | "ṣr9" | "ṣrṣ" | "ṣrṭ" | "ṣrd" | "ṣrb" |
        "ṣr7" | "ṣrq" | "ṣrn" | "ṣrl" | "ṣr3" | "ṣr7" | "ṣr9" | "ṣrṣ" | "ṣrṭ" | "ṣrd" | "ṣrb" |
        "ṣr9" | "ṣrq" | "ṣrn" | "ṣrl" | "ṣr3" | "ṣr7" | "ṣr9" | "ṣrṣ" | "ṣrṭ" | "ṣrd" | "ṣrb" |

        // Emphatic ṣ (ص) with kaf (ك) - expanded
        "ṣkl" | "ṣkq" | "ṣkn" | "ṣk3" | "ṣk7" | "ṣk9" | "ṣkṣ" | "ṣkṭ" | "ṣkd" | "ṣkb" |
        "ṣkb" | "ṣkq" | "ṣkn" | "ṣkl" | "ṣk3" | "ṣk7" | "ṣk9" | "ṣkṣ" | "ṣkṭ" | "ṣkd" | "ṣkf" |
        "ṣkr" | "ṣkq" | "ṣkn" | "ṣkl" | "ṣk3" | "ṣk7" | "ṣk9" | "ṣkṣ" | "ṣkṭ" | "ṣkd" | "ṣkr" |
        "ṣk3" | "ṣkq" | "ṣkn" | "ṣkl" | "ṣk3" | "ṣk7" | "ṣk9" | "ṣkṣ" | "ṣkṭ" | "ṣkd" | "ṣk3" |
        "ṣkhb" | "ṣkhq" | "ṣkhn" | "ṣkhl" | "ṣkh3" | "ṣkh7" | "ṣkh9" | "ṣkhṣ" | "ṣkhṭ" | "ṣkhd" | "ṣkhb" |
        "ṣkhr" | "ṣkhq" | "ṣkhn" | "ṣkhl" | "ṣkh3" | "ṣkh7" | "ṣkh9" | "ṣkhṣ" | "ṣkhṭ" | "ṣkhd" | "ṣkhr" |
        "ṣkhs" | "ṣkhq" | "ṣkhn" | "ṣkhl" | "ṣkh3" | "ṣkh7" | "ṣkh9" | "ṣkhṣ" | "ṣkhṭ" | "ṣkhd" | "ṣkhs" |
        "ṣkhl" | "ṣkhq" | "ṣkhn" | "ṣkhl" | "ṣkh3" | "ṣkh7" | "ṣkh9" | "ṣkhṣ" | "ṣkhṭ" | "ṣkhd" | "ṣkhl" |
        "ṣkm" | "ṣkmq" | "ṣkmn" | "ṣkml" | "ṣkm3" | "ṣkm7" | "ṣkm9" | "ṣkmṣ" | "ṣkmṭ" | "ṣkmd" | "ṣkmb" |
        "ṣkm3" | "ṣkmq" | "ṣkmn" | "ṣkml" | "ṣkm3" | "ṣkm7" | "ṣkm9" | "ṣkmṣ" | "ṣkmṭ" | "ṣkmd" | "ṣkm3" |
        "ṣkn" | "ṣknq" | "ṣknn" | "ṣknl" | "ṣkn3" | "ṣkn7" | "ṣkn9" | "ṣknṣ" | "ṣknṭ" | "ṣknd" | "ṣknb" |
        "ṣk3l" | "ṣk3q" | "ṣk3n" | "ṣk3l" | "ṣk33" | "ṣk37" | "ṣk39" | "ṣk3ṣ" | "ṣk3ṭ" | "ṣk3d" | "ṣk3b" |
        "ṣk3n" | "ṣk3q" | "ṣk3n" | "ṣk3l" | "ṣk33" | "ṣk37" | "ṣk39" | "ṣk3ṣ" | "ṣk3ṭ" | "ṣk3d" | "ṣk3n"
    )
}

/// Simple check for function words - basic implementation
/// In a real system, this would be a proper database
pub fn is_function_word(word: &str) -> bool {
    let word = word.to_lowercase();

    // Common function words that rarely use emphatic forms
    matches!(
        word.as_str(),
        "men"
            | "min"
            | "fi"
            | "f"
            | "3la"
            | "3l"
            | "b"
            | "bl"
            | "l"
            | "li"
            | "la"
            | "ln"
            | "lchi"
            | "l7e"
            | "lkhe"
            | "lka"
            | "lkan"
            | "lkin"
            | "lma"
            | "lman"
            | "lmen"
            | "lmi"
            | "lmor"
            | "lmra"
            | "lm3a"
            | "l3ech"
            | "l9ed"
            | "l9od"
            | "lli"
            | "ll"
            | "kan"
            | "k"
            | "nta"
            | "nti"
            | "ntouma"
            | "7na"
            | "houa"
            | "houma"
            | "ana"
            | "ach"
            | "wech"
            | "wach"
            | "fin"
            | "fash"
            | "3ach"
            | "alach"
            | "lach"
            | "ila"
            | "yak"
            | "ma"
            | "mach"
            | "machi"
    )
}

/// Aspect markers that indicate non-emphatic forms
pub static ASPECT_MARKERS: [&str; 6] = ["ta", "tay", "ti", "tina", "tou", "tiou"];

/// Check if a word starts with an aspect marker (indicates non-emphatic)
pub fn starts_with_aspect_marker(word: &str) -> bool {
    let word = word.to_lowercase();
    ASPECT_MARKERS.iter().any(|marker| word.starts_with(marker))
}

/// Extract a simple root from a word (basic pattern matching)
/// This is a simplified root extraction for Darija
pub fn extract_root(word: &str) -> Option<String> {
    let word = word.to_lowercase();

    // Remove common suffixes first
    let mut word_root = word.strip_suffix("at").unwrap_or(&word);
    word_root = word_root.strip_suffix("a").unwrap_or(word_root);
    word_root = word_root.strip_suffix("i").unwrap_or(word_root);
    word_root = word_root.strip_suffix("in").unwrap_or(word_root);
    word_root = word_root.strip_suffix("an").unwrap_or(word_root);

    // Remove common aspect marker prefixes (check longer ones first)
    let word_no_prefix = word_root.strip_prefix("tay").unwrap_or(word_root);
    let word_no_prefix = word_no_prefix.strip_prefix("ka").unwrap_or(word_no_prefix);

    // Extract consonantal root by removing vowels
    let consonants: String = word_no_prefix
        .chars()
        .filter(|c| !matches!(c, 'a' | 'i' | 'u' | 'e' | 'o'))
        .collect();

    // Basic 3-letter root extraction
    if consonants.len() >= 3 {
        Some(consonants.chars().take(3).collect())
    } else if consonants.len() >= 2 {
        Some(consonants.to_string())
    } else {
        None
    }
}

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

    #[test]
    fn test_emphatic_lexicon() {
        assert!(should_be_emphatic_t("ṭbi3"));
        assert!(should_be_emphatic_t("TB3"));
        assert!(!should_be_emphatic_t("tabi3"));
        assert!(!should_be_emphatic_t("tab3"));
    }

    #[test]
    fn test_emphatic_roots() {
        assert!(should_be_emphatic_s("ṭb3"));
        assert!(should_be_emphatic_s("TB3"));
        assert!(!should_be_emphatic_s("tb3"));
        assert!(should_be_emphatic_s("ṣb7"));
        assert!(should_be_emphatic_s("SB7"));
        assert!(!should_be_emphatic_s("sb7"));
    }

    #[test]
    fn test_function_words() {
        assert!(is_function_word("men"));
        assert!(is_function_word("fi"));
        assert!(is_function_word("3la"));
        assert!(is_function_word("lli"));
        assert!(!is_function_word("ṭbi3"));
        assert!(!is_function_word("ktab"));
    }

    #[test]
    fn test_aspect_markers() {
        assert!(starts_with_aspect_marker("tabi3"));
        assert!(starts_with_aspect_marker("tayeb"));
        assert!(starts_with_aspect_marker("tiina"));
        assert!(starts_with_aspect_marker("touka"));
        assert!(starts_with_aspect_marker("tiouma"));
        assert!(!starts_with_aspect_marker("ṭbi3"));
        assert!(!starts_with_aspect_marker("ktab"));
    }

    #[test]
    fn test_root_extraction() {
        assert_eq!(extract_root("tabi3a"), Some("tb3".to_string()));
        assert_eq!(extract_root("ktab"), Some("ktb".to_string()));
        assert_eq!(extract_root("mchawi"), Some("mch".to_string()));
        assert_eq!(extract_root("sa"), None);
    }
}
