import sys
import 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 list_all_threads():
    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.goto("https://muse.ai", wait_until="domcontentloaded", timeout=30000)
        time.sleep(3)
        
        # Expand "Đoạn chat phụ" if collapsible
        sub_chat_toggle = page.locator('button:has-text("Đoạn chat phụ")').first
        if sub_chat_toggle.count() > 0:
            sub_chat_toggle.click()
            time.sleep(1)
            
        # Get all text in sidebar
        sidebar = page.locator('aside, nav, [role="navigation"], .sidebar').first
        if sidebar.count() > 0:
            print("--- SIDEBAR TEXT ---")
            print(sidebar.inner_text())
            
        # Also check all links
        links = page.locator('a').all()
        print(f"\n--- ALL LINKS ({len(links)}) ---")
        for i, l in enumerate(links):
            h = l.get_attribute("href") or ""
            t = l.inner_text().strip().replace("\n", " ")
            if "/thread/" in h or "chat" in h:
                print(f"[{i}] {h} -> '{t}'")

if __name__ == "__main__":
    list_all_threads()
