import sys, time
from pathlib import Path
sys.path.append(str(Path("05_Production_Pipeline").resolve()))
from invisible_playwright import InvisiblePlaywright
from muse_invpw_driver import sync_worker_profile, DEFAULT_PROFILE

def run():
    sync_worker_profile()
    with InvisiblePlaywright(profile_dir=DEFAULT_PROFILE, headless=True) as ctx:
        page = ctx.pages[0] if ctx.pages else ctx.new_page()
        page.on('console', lambda msg: print(f"[CONSOLE {msg.type}] {msg.text}"))
        page.on('pageerror', lambda exc: print(f"[PAGE ERROR] {exc}"))
        
        page.goto('https://muse.ai', wait_until='domcontentloaded', timeout=30000)
        time.sleep(4)
        
        print("Current URL:", page.url)
        
        # Check all video elements
        videos = page.locator('video').all()
        print('Video count:', len(videos))
        for i, v in enumerate(videos):
            src = v.get_attribute("src")
            current_src = v.evaluate("el => el.currentSrc")
            ready = v.evaluate("el => el.readyState")
            err = v.evaluate("el => el.error ? el.error.message : null")
            print(f"Video {i}: src={src}, currentSrc={current_src}, readyState={ready}, error={err}")

        # Check 'Không tìm thấy video'
        nf = page.locator('text="Không tìm thấy video"').all()
        print('Not found count:', len(nf))
        for el in nf:
            html = el.evaluate("el => el.parentElement.parentElement.outerHTML")
            print("Element HTML:", html[:500])

if __name__ == '__main__':
    run()
