#!/usr/bin/env python3
"""
Systematic debugging to understand why long vowel detection isn't working.
We'll test very simple cases and try to isolate the exact stage where things go wrong.
"""

import cadar


def debug_step_by_step():
    print("=== SYSTEMATIC DEBUG: Step by Step Analysis ===\n")

    # Test very simple cases
    test_cases = [
        ("a", "Single a"),
        ("aa", "Double a - should become A2 → اا"),
        ("l", "Single l"),
        ("la", "l + a"),
        ("laa", "l + aa - should become ل + A2 → لاا"),
    ]

    print("Testing Latin to Arabic step by step:")
    print("=" * 60)

    for text, description in test_cases:
        print(f"\nTest: {description}")
        print(f"Input: '{text}'")

        try:
            result = cadar.bizi2ara(text, darija="Ma")
            print(f"Output: '{result}'")
            print(f"Length: {len(text)} → {len(result)}")

            # Analyze what happened
            if len(text) == len(result):
                print("✅ Length preserved")
            elif len(result) < len(text):
                print("❌ Length reduced - characters lost")
            elif len(result) > len(text):
                print("⚠️  Length increased - expansion occurred")

            # Expected vs Actual
            if text == "aa":
                expected = "اا"
                if result == expected:
                    print("✅ Perfect! aa → اا")
                elif result == "ا":
                    print("❌ Wrong: aa → ا (should be اا)")
                else:
                    print(f"❌ Unexpected: aa → {result}")

            elif text == "laa":
                expected = "لاا"  # l + aa
                if result == expected:
                    print("✅ Perfect! laa → لاا")
                elif result == "لا":
                    print(f"❌ Wrong: laa → لا (should be لاا)")
                elif result == "الا":
                    print(f"❌ Interesting: laa → الا (al + a instead of l + aa)")
                else:
                    print(f"❌ Unexpected: laa → {result}")

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

    print("\n" + "=" * 60)
    print("Testing Arabic to Latin step by step:")
    print("=" * 60)

    arabic_test_cases = [
        ("ا", "Single ا"),
        ("اا", "Double اا - should become aa"),
        ("ل", "Single ل"),
        ("لا", "ل + ا"),
        ("لاا", "ل + اا - should become l + aa → laa"),
    ]

    for text, description in arabic_test_cases:
        print(f"\nTest: {description}")
        print(f"Input: '{text}'")

        try:
            result = cadar.ara2bizi(text, darija="Ma")
            print(f"Output: '{result}'")
            print(f"Length: {len(text)} → {len(result)}")

            # Analyze what happened
            if len(text) == len(result):
                print("✅ Length preserved")
            elif len(result) < len(text):
                print("❌ Length reduced - characters lost")
            elif len(result) > len(text):
                print("⚠️  Length increased - expansion occurred")

            # Expected vs Actual
            if text == "اا":
                expected = "aa"
                if result == expected:
                    print("✅ Perfect! اا → aa")
                elif result == "a":
                    print("❌ Wrong: اا → a (should be aa)")
                else:
                    print(f"❌ Unexpected: اا → {result}")

            elif text == "لاا":
                expected = "laa"
                if result == expected:
                    print("✅ Perfect! لاا → laa")
                elif result == "la":
                    print("❌ Wrong: لاا → la (should be laa)")
                else:
                    print(f"❌ Unexpected: لاا → {result}")

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


if __name__ == "__main__":
    debug_step_by_step()
