import urllib.request
import json
import ssl

def get_vertexdc_users():
    api_url = "https://api.sibu.vertexdc.com"
    login_data = json.dumps({
        "email": "admin@sibu.com",
        "password": "admin",
        "keep_session": True
    }).encode('utf-8')
    
    # Bypass SSL verification if needed (not recommended but for quick dev check)
    ctx = ssl.create_default_context()
    ctx.check_hostname = False
    ctx.verify_mode = ssl.CERT_NONE

    print(f"Logging in to {api_url}/api/auth/login...")
    try:
        req = urllib.request.Request(f"{api_url}/api/auth/login", data=login_data, headers={'Content-Type': 'application/json'})
        with urllib.request.urlopen(req, context=ctx) as response:
            token_data = json.loads(response.read().decode('utf-8'))
            token = token_data.get("access_token")
            print("Login successful!")
        
        headers = {"Authorization": f"Bearer {token}"}
        
        print("Fetching users via /api/users/search?email=...")
        # Search with an empty string to get all users
        search_url = f"{api_url}/api/users/search?email="
        req_search = urllib.request.Request(search_url, headers=headers)
        
        with urllib.request.urlopen(req_search, context=ctx) as response:
            users = json.loads(response.read().decode('utf-8'))
            print(f"Found {len(users)} users:")
            for u in users:
                print(f"- {u['full_name']} ({u['email']}) Role: {u['role']}")
                
    except Exception as e:
        print(f"Error: {e}")

if __name__ == "__main__":
    get_vertexdc_users()
