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:
        act.click()
        time.sleep(3)
        
    # Inspect the MP4 card container
    card = page.locator('span[title*="media-generation-toan-canh-thap-nien-1980"]').first
    print("Found card title:", card.count() > 0)
    
    # Let's inspect the surrounding DOM of this card
    html_info = card.evaluate('''el => {
        let cur = el;
        const parents = [];
        for (let i = 0; i < 5; i++) {
            if (!cur) break;
            parents.push({
                tag: cur.tagName,
                className: cur.className,
                html: cur.outerHTML.slice(0, 400),
                buttons: Array.from(cur.querySelectorAll('button')).map(b => ({
                    text: b.innerText,
                    ariaLabel: b.getAttribute('aria-label'),
                    title: b.getAttribute('title'),
                    className: b.className
                })),
                links: Array.from(cur.querySelectorAll('a')).map(a => ({
                    href: a.href,
                    download: a.download,
                    text: a.innerText
                }))
            });
            cur = cur.parentElement;
        }
        return parents;
    }''')
    
    import json
    print(json.dumps(html_info, indent=2, ensure_ascii=False))
