#!/usr/bin/env python3
"""Check future bookings in Launch27 by searching"""
import sys
import asyncio
from playwright.async_api import async_playwright

EMAIL = "mike@nomorechores.com"
PASSWORD = "er!NCY73J"
BASE_URL = "https://nomorechores.launch27.com"
CUSTOMER_EMAIL = sys.argv[1] if len(sys.argv) > 1 else "sjg32420@outlook.com"

async def check():
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=True)
        page = await browser.new_page()
        page.set_default_timeout(30000)

        await page.goto(f"{BASE_URL}/login", wait_until="networkidle")
        await page.wait_for_timeout(2000)
        await page.fill('input[ng-model="ctrl.user.email"]', EMAIL)
        await page.fill('input[ng-model="ctrl.user.password"]', PASSWORD)
        await page.click('button:has-text("Sign In")')
        await page.wait_for_load_state("networkidle")
        await page.wait_for_timeout(2000)

        # Search bookings with email filter
        url = f"{BASE_URL}/admin/bookings?q={CUSTOMER_EMAIL.replace('@','%40')}"
        await page.goto(url, wait_until="domcontentloaded")
        await page.wait_for_timeout(4000)
        text = await page.evaluate("() => document.body.innerText")
        print(f"Bookings search:\n{text[:1500]}")
        await page.screenshot(path="/Users/harvey/.openclaw/workspace/l27-future-bookings.png")
        await browser.close()

asyncio.run(check())
