import sys
import time
from pathlib import Path
from invisible_playwright import InvisiblePlaywright

if sys.platform == "win32":
    sys.stdout.reconfigure(encoding="utf-8", line_buffering=True)
    sys.stderr.reconfigure(encoding="utf-8", line_buffering=True)

pdir = Path.home() / 'AntigravityProfiles' / 'phu2234_worker'

with InvisiblePlaywright(profile_dir=pdir, headless=True) as ctx:
    page = ctx.pages[0] if ctx.pages else ctx.new_page()
    page.goto('https://muse.ai', wait_until='networkidle', timeout=30000)
    time.sleep(3)
    
    # Click activity item
    act = page.locator('text="Tạo video điện ảnh 10s thập niên 1980"').first
    if act.count() > 0:
        print("Found activity item, clicking...")
        act.click()
        time.sleep(3)
    else:
        print("Activity item not found directly, looking in activity panel...")
        
    # Inspect elements that contain MP4 or media-generation
    info = page.evaluate('''() => {
        const matches = [];
        const all = document.querySelectorAll('*');
        for (const el of all) {
            if (el.children.length === 0 && (el.innerText?.includes('MP4') || el.innerText?.includes('media-generation'))) {
                matches.push({
                    tag: el.tagName,
                    text: el.innerText,
                    parentTag: el.parentElement?.tagName,
                    parentHTML: el.parentElement?.outerHTML?.slice(0, 400),
                    grandParentHTML: el.parentElement?.parentElement?.outerHTML?.slice(0, 500)
                });
            }
        }
        return matches;
    }''')
    
    print(f"Found {len(info)} matching elements:")
    for idx, m in enumerate(info):
        print(f"--- Match {idx} ---")
        print(f"Tag: {m['tag']}, Text: {m['text']}")
        print(f"Parent HTML: {m['parentHTML']}")
        print(f"Grandparent HTML: {m['grandParentHTML']}")
