#!/usr/bin/env python3
"""
Debug script to see what normalization is doing to our text before it reaches ICR.
This will help us understand if normalization is destroying long vowels.
"""

import cadar
import sys


def test_normalization():
    print("=== DEBUG: Testing Normalization Impact ===\n")

    # Test cases that should preserve long vowels
    test_cases = [
        "aa",  # Should stay "aa"
        "ii",  # Should stay "ii"
        "uu",  # Should stay "uu"
        "oo",  # Should stay "oo"
        "لاا",  # Should stay "لاا"
        "ييي",  # Should become "يي" (if normalization limits to 2)
        "ووو",  # Should become "وو" (if normalization limits to 2)
        "laa",  # Should preserve "aa"
        "lii",  # Should preserve "ii"
        "luu",  # Should preserve "uu"
        "loo",  # Should preserve "oo"
    ]

    print("Testing normalization impact on long vowels:")
    print("=" * 50)

    for test_input in test_cases:
        print(f"\nInput: '{test_input}'")

        try:
            if all(ord(c) < 128 for c in test_input):  # Latin script
                # Latin to Arabic
                arabic_result = cadar.bizi2ara(test_input, darija="Ma")
                print(f"  Latin → Arabic: '{test_input}' → '{arabic_result}'")

                # Try to convert back
                if arabic_result != test_input:  # Only convert back if different
                    latin_result = cadar.ara2bizi(arabic_result, darija="Ma")
                    print(f"  Arabic → Latin: '{arabic_result}' → '{latin_result}'")

                    # Check what we lost
                    if test_input == latin_result:
                        print("  ✅ Roundtrip: SUCCESS")
                    else:
                        print(f"  ❌ Roundtrip: FAILED")
                        print(f"     Expected: '{test_input}'")
                        print(f"     Got:      '{latin_result}'")
                        print(
                            f"     Lost:     '{test_input.replace(latin_result, '')}'"
                            if len(test_input) > len(latin_result)
                            else ""
                        )
                else:
                    print("  ⚠️  No conversion occurred")

            else:  # Arabic script
                # Arabic to Latin
                latin_result = cadar.ara2bizi(test_input, darija="Ma")
                print(f"  Arabic → Latin: '{test_input}' → '{latin_result}'")

                # Check what we lost
                if test_input in latin_result or len(test_input) == len(latin_result):
                    print("  ✅ Length preserved or expanded")
                else:
                    print(
                        f"  ❌ Length reduced: {len(test_input)} → {len(latin_result)} chars"
                    )
                    print(
                        f"     Expected something like: '{test_input}' → should contain '{test_input}'"
                    )

        except Exception as e:
            print(f"  ❌ ERROR: {e}")


if __name__ == "__main__":
    test_normalization()
