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 DEFAULT_PROFILE

def inspect_threads():
    with InvisiblePlaywright(profile_dir=DEFAULT_PROFILE, headless=True) as ctx:
        page = ctx.pages[0] if ctx.pages else ctx.new_page()
        page.goto("https://muse.ai", wait_until="domcontentloaded", timeout=30000)
        time.sleep(5)
        
        page.screenshot(path="04_Assets/current_muse_home.png")
        print(f"Current URL: {page.url}")
        
        # List sidebar threads
        threads = page.locator('nav a, aside a, [role="navigation"] a, a[href*="/thread/"]')
        print(f"Sidebar threads count: {threads.count()}")
        for i in range(min(threads.count(), 10)):
            t = threads.nth(i)
            print(f"  Thread {i}: text='{t.inner_text().strip()}', href='{t.get_attribute('href')}'")
            
        # Check current page messages and videos
        videos = page.locator('video')
        print(f"Videos count on current page: {videos.count()}")
        for i in range(videos.count()):
            v = videos.nth(i)
            src = v.get_attribute("src")
            print(f"  Video {i}: src={src[:80] if src else 'None'}")
            
        dl_btns = page.locator('button[aria-label*="Tải video xuống"], button[data-pel="media_download"]')
        print(f"Download buttons count: {dl_btns.count()}")
        
        # Check if there is '⚡ Đang kết nối...' or 'Đã kết nối'
        mascot_text = page.locator('aside, [class*="mascot"], [class*="sidebar"]').inner_text()
        first_few_lines = "\n".join(mascot_text.splitlines()[:10])
        print(f"Sidebar info:\n{first_few_lines}")

if __name__ == '__main__':
    inspect_threads()
