#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
DisplayName Fuzzy Matching Test
================================

Test script to verify the display name variant matching logic.
"""

import sys
import codecs

# Set stdout to UTF-8 for Windows console
if sys.platform == "win32":
    sys.stdout = codecs.getwriter("utf-8")(sys.stdout.buffer, 'strict')
    sys.stderr = codecs.getwriter("utf-8")(sys.stderr.buffer, 'strict')

from crm_api import CRMClient


def test_display_name_variants():
    """Test the get_display_name_variants method"""

    print("="*60)
    print("Testing DisplayName Variant Generation")
    print("="*60)

    # Create a dummy CRMClient (we only need the method, not real connection)
    client = CRMClient("https://example.com", {"ReqClientId": "test", "orgId": "test"})

    test_cases = [
        ("John Smith-d", ["John Smith-d", "John Smith"]),
        ("John Smith", ["John Smith", "John Smith-d"]),
        ("Jane Doe-d", ["Jane Doe-d", "Jane Doe"]),
        ("Bob Johnson", ["Bob Johnson", "Bob Johnson-d"]),
        ("Alice-d", ["Alice-d", "Alice"]),
        ("Charlie Brown-d ", ["Charlie Brown-d", "Charlie Brown"]),  # Trailing space is stripped
    ]

    print("\nTest Cases:\n")

    all_passed = True

    for input_name, expected_variants in test_cases:
        result = client.get_display_name_variants(input_name)

        passed = result == expected_variants
        status = "✓ PASS" if passed else "✗ FAIL"

        print(f"{status}: '{input_name}'")
        print(f"  Expected: {expected_variants}")
        print(f"  Got:      {result}")

        if not passed:
            all_passed = False
        print()

    print("="*60)
    if all_passed:
        print("✓ All tests passed!")
    else:
        print("✗ Some tests failed!")
    print("="*60)

    return all_passed


def test_edge_cases():
    """Test edge cases"""

    print("\n" + "="*60)
    print("Testing Edge Cases")
    print("="*60 + "\n")

    client = CRMClient("https://example.com", {"ReqClientId": "test", "orgId": "test"})

    # Edge case 1: Multiple -d suffixes
    print("1. Multiple -d suffixes:")
    result = client.get_display_name_variants("John Smith-d-d")
    print(f"   Input:  'John Smith-d-d'")
    print(f"   Result: {result}")
    print()

    # Edge case 2: Empty string
    print("2. Empty string:")
    result = client.get_display_name_variants("")
    print(f"   Input:  ''")
    print(f"   Result: {result}")
    print()

    # Edge case 3: Only "-d"
    print("3. Only '-d':")
    result = client.get_display_name_variants("-d")
    print(f"   Input:  '-d'")
    print(f"   Result: {result}")
    print()

    # Edge case 4: Name with spaces before -d
    print("4. Spaces before -d:")
    result = client.get_display_name_variants("John Smith -d")
    print(f"   Input:  'John Smith -d'")
    print(f"   Result: {result}")
    print()

    # Edge case 5: Uppercase -D
    print("5. Uppercase -D suffix:")
    result = client.get_display_name_variants("John Smith-D")
    print(f"   Input:  'John Smith-D'")
    print(f"   Result: {result}")
    print(f"   Note: Script only checks for lowercase '-d'")
    print()


def main():
    """Run all tests"""

    print("\n" + "="*60)
    print("DISPLAYNAME FUZZY MATCHING TEST SUITE")
    print("="*60 + "\n")

    # Run tests
    test_display_name_variants()
    test_edge_cases()

    print("\n" + "="*60)
    print("Test suite completed!")
    print("="*60)


if __name__ == "__main__":
    main()
