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_unread_thread():
    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)
        
        # Click on 'Tạo video Thuy Kieu hoàng hôn'
        thread = page.locator(':text("Tạo video Thuy Kieu hoàng hôn"), :text("Tạo video Thuy Kieu ho")').first
        if thread.count() > 0:
            print("Clicking thread:", thread.inner_text())
            thread.click()
            time.sleep(4)
            
        page.screenshot(path="04_Assets/unread_thread_view.png", full_page=True)
        print("Page URL:", page.url)
        
        # Check download buttons
        dl_btns = page.locator('button:has-text("Tải video xuống"), button[aria-label*="Tải video xuống"], button[data-pel="media_download"]').all()
        print(f"DL buttons found: {len(dl_btns)}")
        
        # Scroll to bottom
        page.evaluate("window.scrollTo(0, document.body.scrollHeight)")
        time.sleep(2)
        page.screenshot(path="04_Assets/unread_thread_bottom.png")
        
        # Print latest 3 assistant messages
        msgs = page.locator('.chat-message, [role="article"]').all_inner_texts()
        print(f"Total messages: {len(msgs)}")
        for i, m in enumerate(msgs[-3:]):
            print(f"--- Message {i+1} ---")
            print(m[:300])

if __name__ == '__main__':
    inspect_unread_thread()
