#!/usr/bin/env python3
import os, sys, time
from playwright.sync_api import sync_playwright

def run():
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=True)
        context = browser.new_context(
            user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
        )
        page = context.new_page()
        
        try:
            print("Navigating to VentraIP VIPControl...")
            page.goto('https://vip.ventraip.com.au/login/', timeout=60000)
            page.wait_for_load_state('networkidle')
            page.screenshot(path='reports/ventraip_login_page.png')
            
            print("Filling credentials...")
            # Detect selectors
            page.fill('input[name="email"]', 'kinan@protonmail.com')
            page.fill('input[name="password"]', '9iphQKcIv0#2J0Ce')
            
            page.screenshot(path='reports/ventraip_filled.png')
            
            # Click login - often a button with text "Login" or type="submit"
            login_btn = page.query_selector('button:has-text("Login")') or page.query_selector('input[type="submit"]')
            if login_btn:
                login_btn.click()
                print("Login clicked.")
            else:
                page.keyboard.press('Enter')
                print("Pressed Enter.")
                
            page.wait_for_timeout(5000)
            page.screenshot(path='reports/ventraip_after_login.png')
            print(f"URL after login: {page.url}")
            
        except Exception as e:
            print(f"Error: {e}")
            page.screenshot(path='reports/ventraip_error.png')
        finally:
            browser.close()

if __name__ == '__main__':
    os.makedirs('reports', exist_ok=True)
    run()
