#!/usr/bin/env python3
"""Test GHL login directly"""
import os
from playwright.sync_api import sync_playwright

os.environ['LD_LIBRARY_PATH'] = '/mnt/e/genesis-system/.venvs/playwright-libs/'

# Try multiple credential variants
attempts = [
    ('kinan@agileadpt.com', 'SystemBird505*'),
    ('kinan@agileadapt.com', 'SystemBird505*'),
    ('kinan@agileadapt.io', 'SystemBird505*'),
]

with sync_playwright() as p:
    browser = p.chromium.launch(headless=False, args=['--no-sandbox'])  # headless=False to see what's happening
    page = browser.new_page(viewport={'width':1920,'height':1080})
    
    page.goto('https://app.gohighlevel.com/', timeout=30000)
    page.wait_for_selector('input[name="email"]', timeout=15000)
    
    print("Testing login attempts...")
    for email, password in attempts:
        print(f"\nAttempting: {email} / {password[:10]}...")
        page.fill('input[name="email"]', email)
        page.fill('input[name="password"]', password)
        page.screenshot(path=f'/tmp/ghl_before_{email.split("@")[0]}.png')
        page.click('button[type="submit"]')
        
        try:
            # Wait for either success or error
            page.wait_for_load_state('networkidle', timeout=10000)
            print(f"  URL after submit: {page.url}")
            page.screenshot(path=f'/tmp/ghl_after_{email.split("@")[0]}.png')
            
            # Check for error messages
            content = page.content()
            if 'error' in content.lower() or 'invalid' in content.lower():
                print(f"  ERROR DETECTED in response")
            elif 'location' in page.url:
                print(f"  SUCCESS!")
                break
            
        except Exception as e:
            print(f"  Exception: {e}")
        
        # Reset for next attempt
        page.goto('https://app.gohighlevel.com/', timeout=30000)
        page.wait_for_selector('input[name="email"]', timeout=15000)
        page.wait_for_timeout(500)
    
    print("\nOpening browser for manual inspection - close when done")
    input("Press Enter to close...")
    browser.close()
