"""
Manual verification script to demonstrate the backward-compatible enum solution.

This script can be run to manually verify that:
1. The custom Enum class works correctly
2. The sed transformations work on the actual schema
3. Unknown enum values are handled gracefully after regeneration

Run this with: python -m pytest tests/lib/test_manual_verification.py -v -s
"""

import logging
import re
from pathlib import Path
from unittest import TestCase


class TestManualVerification(TestCase):
    """Manual verification tests that can be run to see the solution in action."""

    def test_demonstrate_custom_enum_with_unknown_values(self):
        """
        Demonstrate how the custom Enum handles unknown values.
        This shows what will happen after schema regeneration.
        """
        print("\n" + "=" * 70)
        print("DEMONSTRATION: Custom Enum with Unknown Values")
        print("=" * 70)

        import sgqlc.types

        from pycarlo.lib.types import Enum

        # Create a test enum
        class DemoEnum(Enum):
            __schema__ = sgqlc.types.Schema()
            __choices__ = ("KNOWN_VALUE_1", "KNOWN_VALUE_2", "KNOWN_VALUE_3")

        # Configure logging to see warnings
        logging.basicConfig(level=logging.WARNING, format="%(levelname)s: %(message)s")

        print("\n1. Testing known values (no warnings):")
        known = DemoEnum("KNOWN_VALUE_1")
        print(f"   DemoEnum('KNOWN_VALUE_1') = {known}")
        print(f"   Type: {type(known)}")
        print(f"   Equals 'KNOWN_VALUE_1': {known == 'KNOWN_VALUE_1'}")

        print("\n2. Testing unknown value (will log warning):")
        unknown = DemoEnum("UNKNOWN_FUTURE_VALUE")
        print(f"   DemoEnum('UNKNOWN_FUTURE_VALUE') = {unknown}")
        print(f"   Type: {type(unknown)}")
        print(f"   Equals 'UNKNOWN_FUTURE_VALUE': {unknown == 'UNKNOWN_FUTURE_VALUE'}")

        print("\n3. Testing mixed values (simulating API response):")
        api_response = ["KNOWN_VALUE_1", "UNKNOWN_VALUE_A", "KNOWN_VALUE_2", "UNKNOWN_VALUE_B"]
        deserialized = [DemoEnum(v) for v in api_response]
        print(f"   API response: {api_response}")
        print(f"   Deserialized: {deserialized}")
        print(f"   All values preserved: {deserialized == api_response}")

        print("\n4. Filtering known vs unknown values:")
        known_only = [v for v in deserialized if v in DemoEnum]
        unknown_only = [v for v in deserialized if v not in DemoEnum]
        print(f"   Known values: {known_only}")
        print(f"   Unknown values: {unknown_only}")

        print("\n" + "=" * 70)
        print("✅ Demonstration complete!")
        print("=" * 70 + "\n")

    def test_demonstrate_schema_transformation(self):
        """
        Demonstrate the transformed schema (already processed by make transform-schema).
        """
        print("\n" + "=" * 70)
        print("DEMONSTRATION: Transformed Schema")
        print("=" * 70)

        schema_path = Path(__file__).parent.parent.parent / "pycarlo" / "lib" / "schema.py"
        content = schema_path.read_text()

        print("\n1. Transformed schema (first 25 lines):")
        print("-" * 70)
        for i, line in enumerate(content.split("\n")[:25], 1):
            print(f"   {i:3d}: {line}")

        # Count transformed enums
        custom_enum_count = len(
            re.findall(r"class [A-Za-z0-9_]*\(pycarlo\.lib\.types\.Enum\):", content)
        )
        old_enum_count = len(re.findall(r"class [A-Za-z0-9_]*\(sgqlc\.types\.Enum\):", content))

        print("\n2. Transformation statistics:")
        print("-" * 70)
        print(f"   Custom enum classes: {custom_enum_count}")
        print(f"   Old sgqlc enum classes: {old_enum_count}")
        print(f"   Import statement added: {'✓' if 'import pycarlo.lib.types' in content else '✗'}")

        print("\n3. Sample transformed enum (EntitlementTypes):")
        print("-" * 70)
        # Find EntitlementTypes
        enum_match = re.search(
            r"(class EntitlementTypes\(pycarlo\.lib\.types\.Enum\):.*?__choices__)",
            content,
            re.DOTALL,
        )
        if enum_match:
            print("   TRANSFORMED:")
            for line in enum_match.group(1).split("\n")[:5]:
                print(f"     {line}")

        print("\n" + "=" * 70)
        print("✅ Schema transformation demonstration complete!")
        print("=" * 70 + "\n")

        # Verify the transformation worked
        self.assertEqual(old_enum_count, 0, "Should have no untransformed enums")
        self.assertGreater(custom_enum_count, 50, "Should have many transformed enums")
