"""Demonstrate the bug in the old multiple_domains() implementation.

The old implementation has a logic bug where it returns True when it finds
a DUPLICATE domain (same domain appearing twice), not when there are multiple
DIFFERENT domains.
"""

import requests.cookies


def old_multiple_domains_buggy(jar):
    """Old BUGGY implementation that checks for duplicate domains, not multiple domains."""
    domains = []
    for cookie in iter(jar):
        if cookie.domain is not None and cookie.domain in domains:
            return True  # BUG: Returns True when finding duplicate, not multiple domains!
        domains.append(cookie.domain)
    return False


def new_multiple_domains_correct(jar):
    """New CORRECT implementation that counts unique domains."""
    domains = set()
    for cookie in iter(jar):
        if cookie.domain is not None:
            domains.add(cookie.domain)
    return len(domains) > 1


def main():
    """Demonstrate the bug in the old implementation."""
    print("=" * 80)
    print("Demonstrating the Bug in Old multiple_domains() Implementation")
    print("=" * 80)
    print()

    # Test Case 1: Single domain with multiple cookies
    print("Test Case 1: Multiple cookies, ONE domain (example.com)")
    print("-" * 80)
    jar1 = requests.cookies.RequestsCookieJar()
    jar1.set("cookie1", "value1", domain="example.com")
    jar1.set("cookie2", "value2", domain="example.com")
    jar1.set("cookie3", "value3", domain="example.com")
    
    old_result1 = old_multiple_domains_buggy(jar1)
    new_result1 = new_multiple_domains_correct(jar1)
    actual_result1 = jar1.multiple_domains()  # Uses the new optimized implementation
    
    print(f"  Cookies: {list(jar1.keys())}")
    print(f"  Domains: {[c.domain for c in jar1]}")
    print(f"  Expected result: FALSE (only one domain)")
    print(f"  Old implementation: {old_result1} {'[WRONG!]' if old_result1 else '[OK]'}")
    print(f"  New implementation: {new_result1} {'[OK]' if not new_result1 else '[WRONG]'}")
    print(f"  Actual (optimized): {actual_result1} {'[OK]' if not actual_result1 else '[WRONG]'}")
    print()

    # Test Case 2: Multiple different domains
    print("Test Case 2: Multiple cookies, MULTIPLE domains")
    print("-" * 80)
    jar2 = requests.cookies.RequestsCookieJar()
    jar2.set("cookie1", "value1", domain="example.com")
    jar2.set("cookie2", "value2", domain="test.com")
    jar2.set("cookie3", "value3", domain="another.com")
    
    old_result2 = old_multiple_domains_buggy(jar2)
    new_result2 = new_multiple_domains_correct(jar2)
    actual_result2 = jar2.multiple_domains()
    
    print(f"  Cookies: {list(jar2.keys())}")
    print(f"  Domains: {[c.domain for c in jar2]}")
    print(f"  Expected result: TRUE (three different domains)")
    print(f"  Old implementation: {old_result2} {'[OK]' if old_result2 else '[WRONG!]'}")
    print(f"  New implementation: {new_result2} {'[OK]' if new_result2 else '[WRONG]'}")
    print(f"  Actual (optimized): {actual_result2} {'[OK]' if actual_result2 else '[WRONG]'}")
    print()

    # Test Case 3: Edge case - order matters for old implementation
    print("Test Case 3: Order matters for old implementation")
    print("-" * 80)
    jar3 = requests.cookies.RequestsCookieJar()
    jar3.set("cookie1", "value1", domain="a.com")
    jar3.set("cookie2", "value2", domain="b.com")
    jar3.set("cookie3", "value3", domain="a.com")  # Duplicate of first domain
    
    old_result3 = old_multiple_domains_buggy(jar3)
    new_result3 = new_multiple_domains_correct(jar3)
    actual_result3 = jar3.multiple_domains()
    
    print(f"  Cookies: {list(jar3.keys())}")
    print(f"  Domains: {[c.domain for c in jar3]}")
    print(f"  Expected result: TRUE (two different domains: a.com and b.com)")
    print(f"  Old implementation: {old_result3} {'[OK by luck!]' if old_result3 else '[WRONG]'}")
    print(f"  New implementation: {new_result3} {'[OK]' if new_result3 else '[WRONG]'}")
    print(f"  Actual (optimized): {actual_result3} {'[OK]' if actual_result3 else '[WRONG]'}")
    print()

    # Test Case 4: Empty jar
    print("Test Case 4: Empty cookie jar")
    print("-" * 80)
    jar4 = requests.cookies.RequestsCookieJar()
    
    old_result4 = old_multiple_domains_buggy(jar4)
    new_result4 = new_multiple_domains_correct(jar4)
    actual_result4 = jar4.multiple_domains()
    
    print(f"  Cookies: {list(jar4.keys())}")
    print(f"  Domains: {[c.domain for c in jar4]}")
    print(f"  Expected result: FALSE (no cookies)")
    print(f"  Old implementation: {old_result4} {'[OK]' if not old_result4 else '[WRONG]'}")
    print(f"  New implementation: {new_result4} {'[OK]' if not new_result4 else '[WRONG]'}")
    print(f"  Actual (optimized): {actual_result4} {'[OK]' if not actual_result4 else '[WRONG]'}")
    print()

    print("=" * 80)
    print("Summary")
    print("=" * 80)
    print()
    print("The old implementation has a LOGIC BUG:")
    print("  [X] It returns True when it finds a DUPLICATE domain (same domain twice)")
    print("  [X] It should return True when there are MULTIPLE DIFFERENT domains")
    print()
    print("In Test Case 1:")
    print("  - All cookies have the same domain (example.com)")
    print("  - Should return FALSE (only one domain)")
    print("  - Old implementation returns TRUE (incorrectly)")
    print("  - It sees 'example.com' twice and thinks that means multiple domains")
    print()
    print("The new implementation is CORRECT:")
    print("  [+] Uses a set to track unique domains")
    print("  [+] Returns True only when len(unique_domains) > 1")
    print("  [+] More efficient O(n) instead of O(n^2)")
    print()


if __name__ == "__main__":
    main()
