import sys
import os
from unittest.mock import MagicMock

# Add the project root to the python path
sys.path.append(os.path.abspath(os.path.dirname(__file__)))

# Mock firebase module
sys.modules["firebase"] = MagicMock()
sys.modules["firebase.firebase"] = MagicMock()

try:
    from utility.googleCustomerMatch import GoogleCustomerMatch
    print("Successfully imported GoogleCustomerMatch")
except ImportError as e:
    print(f"Failed to import GoogleCustomerMatch: {e}")
    sys.exit(1)

def test_methods_exist():
    # Mocking init to avoid credential issues
    original_init = GoogleCustomerMatch.__init__
    def mock_init(self):
        self.client = MagicMock() # Mock client too for create_user_identifier
        self.config_dict = {}
    
    GoogleCustomerMatch.__init__ = mock_init
    
    try:
        gcm = GoogleCustomerMatch()
        
        methods = [
            "search_open_user_lists",
            "search_client_customers",
            "create_user_list",
            "create_data_job",
            "add_data_job_operations",
            "run_job",
            "normalize_and_hash",
            "create_user_identifier"
        ]
        
        for method in methods:
            if hasattr(gcm, method):
                print(f"Method {method} exists")
            else:
                print(f"Method {method} MISSING")
        
        # Test create_user_identifier existence
        if hasattr(gcm, 'create_user_identifier'):
             print("Method create_user_identifier exists")
        else:
             print("Method create_user_identifier MISSING")

        # Test add_data_job_operations logic (mocked)
        print("\nTesting add_data_job_operations logic...")
        # Mock get_type to return MagicMock objects that behave like proto messages
        def mock_get_type(type_name):
            mock_obj = MagicMock()
            if type_name == "UserData":
                mock_obj.user_identifiers = []
            elif type_name == "AddOfflineUserDataJobOperationsRequest":
                mock_obj.resource_name = ""
                mock_obj.enable_partial_failure = False
                mock_obj.operations = []
            return mock_obj
            
        gcm.client.get_type.side_effect = mock_get_type
        
        mock_identifiers = ["mock_id_1", "mock_id_2"]
        gcm.add_data_job_operations("job_resource", mock_identifiers)
        
        # Verify that get_type was called for AddOfflineUserDataJobOperationsRequest
        print("add_data_job_operations executed without error (mocked).")

        # Test normalize_and_hash
        print("\nTesting normalize_and_hash...")
        email = " test@example.com "
        expected_hash = "973dfe463ec85785f5f95af5ba3906eedb2d931c24e69824a89ea65dba4e813b"
        actual_hash = gcm.normalize_and_hash(email)
        
        if actual_hash == expected_hash:
            print(f"Hashing passed: '{email}' -> {actual_hash}")
        else:
            print(f"Hashing FAILED: '{email}' -> {actual_hash} (expected {expected_hash})")

    finally:
        GoogleCustomerMatch.__init__ = original_init

if __name__ == "__main__":
    test_methods_exist()
