// Phase 16: Unicode Support Validation Tests
use domainforge_core::parser::parse_to_graph;

#[test]
fn test_unicode_in_entity_names() {
    // Test various Unicode characters in entity names
    let source = r#"
        Entity "Müller GmbH" in germany
        Entity "北京公司" in china
        Entity "Société Générale" in france
        Entity "Компания" in russia
    "#;

    let result = parse_to_graph(source);

    match result {
        Ok(graph) => {
            assert_eq!(graph.entity_count(), 4, "✅ Unicode in entity names works!");

            // Verify we can find entities with Unicode names
            let entities: Vec<_> = graph
                .all_entities()
                .iter()
                .map(|e| e.name().to_string())
                .collect();

            assert!(entities.contains(&"Müller GmbH".to_string()));
            assert!(entities.contains(&"北京公司".to_string()));
            assert!(entities.contains(&"Société Générale".to_string()));
            assert!(entities.contains(&"Компания".to_string()));
        }
        Err(e) => {
            panic!("❌ Unicode in entity names failed: {:?}\n   Need to implement escape_sequence in grammar", e);
        }
    }
}

#[test]
fn test_unicode_in_resource_names() {
    let source = r#"
        Resource "Schüssel" units
        Resource "カメラ" units
        Resource "Café" kg
    "#;

    let result = parse_to_graph(source);

    match result {
        Ok(graph) => {
            assert_eq!(
                graph.resource_count(),
                3,
                "✅ Unicode in resource names works!"
            );
        }
        Err(e) => {
            panic!("❌ Unicode in resource names failed: {:?}", e);
        }
    }
}

#[test]
fn test_basic_escape_sequences_validation() {
    // Test if basic escape sequences work or need implementation
    let test_cases = vec![
        (r#"Entity "Quote: \"test\"" "#, "Quote: \"test\""),
        (r#"Entity "Line\nBreak" "#, "Line\nBreak"),
        (r#"Entity "Tab\there" "#, "Tab\there"),
        (r#"Entity "Back\\slash" "#, "Back\\slash"),
    ];

    let mut working = vec![];
    let mut broken = vec![];

    for (source, expected_name) in test_cases {
        match parse_to_graph(source) {
            Ok(graph) => {
                let entity_name = graph
                    .all_entities()
                    .first()
                    .map(|e| e.name().to_string())
                    .unwrap_or_default();

                if entity_name == expected_name {
                    working.push(expected_name);
                } else {
                    broken.push(format!("{} (got: {})", expected_name, entity_name));
                }
            }
            Err(_) => broken.push(expected_name.to_string()),
        }
    }

    if !working.is_empty() {
        println!("✅ Working escapes: {:?}", working);
    }
    if !broken.is_empty() {
        println!("❌ Broken escapes: {:?}", broken);
        println!("   Need to implement escape_sequence rule in grammar");
    }

    // Don't fail the test - this is validation only
    // If all escapes work, that's great. If not, we document what needs implementing.
}

#[test]
fn test_emoji_and_special_unicode() {
    let source = r#"
        Entity "Factory 🏭" in production
        Entity "Warehouse 📦" in logistics
        Resource "Money 💰" units
    "#;

    let result = parse_to_graph(source);

    match result {
        Ok(graph) => {
            assert_eq!(graph.entity_count(), 2, "✅ Emoji in names works!");
            assert_eq!(
                graph.resource_count(),
                1,
                "✅ Emoji in resource names works!"
            );
        }
        Err(e) => {
            println!("❌ Emoji support failed: {:?}", e);
            println!("   This is expected - emoji may need special handling");
        }
    }
}

#[test]
fn test_mixed_unicode_and_ascii() {
    let source = r#"
        Entity "ABC Company (中文)" in business
        Resource "Parts-零件" kg
        Flow "Parts-零件" from "ABC Company (中文)" to "ABC Company (中文)" quantity 100
    "#;

    let result = parse_to_graph(source);

    match result {
        Ok(graph) => {
            assert_eq!(graph.entity_count(), 1);
            assert_eq!(graph.resource_count(), 1);
            assert_eq!(graph.flow_count(), 1);
            println!("✅ Mixed ASCII/Unicode works perfectly!");
        }
        Err(e) => {
            panic!("❌ Mixed ASCII/Unicode failed: {:?}", e);
        }
    }
}

#[test]
fn test_unicode_in_namespaces() {
    let source = r#"
        Entity "Company" in münchen
        Resource "Product" kg in 北京
    "#;

    let result = parse_to_graph(source);

    // Namespaces might have restrictions, so we just check if it parses
    match result {
        Ok(_) => println!("✅ Unicode in namespaces works!"),
        Err(_) => println!("⚠️ Unicode in namespaces may have restrictions (this is OK)"),
    }
}
