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 list_sidebar_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(3)
        
        threads = page.evaluate("""() => {
            const links = Array.from(document.querySelectorAll('a, button, [role="button"], div')).map(el => {
                const text = el.innerText ? el.innerText.trim() : '';
                const href = el.getAttribute('href');
                return { text, href, tag: el.tagName };
            });
            return links.filter(l => l.href && (l.href.includes('thread') || l.href.includes('chat')));
        }""")
        print("Threads found via links:", threads)
        
        # Check text in sidebar
        sidebar_text = page.locator('nav, aside, [class*="sidebar"]').all_inner_texts()
        print("Sidebar text:", sidebar_text)

if __name__ == '__main__':
    list_sidebar_threads()
