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 inspect_chat_actions():
    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)
        
        # Hover over thread item
        thread_item = page.locator(':text("Tạo video Thuy Kieu")').first
        if thread_item.count() > 0:
            print("Hovering over thread item...")
            thread_item.hover()
            time.sleep(1)
            
        # Find action buttons
        action_btns = page.locator('button[aria-label*="cuộc trò chuyện"], button[aria-label*="Tùy chọn"], button[aria-label*="Hành động"]').all()
        print(f"Action buttons: {len(action_btns)}")
        for b in action_btns:
            print("Action btn:", b.get_attribute("aria-label"))
            
        if action_btns:
            action_btns[-1].click()
            time.sleep(1)
            menu_items = page.locator('[role="menuitem"], [role="menu"] button, [role="dialog"] button').all()
            print(f"Menu items: {len(menu_items)}")
            for m in menu_items:
                print("Menu item:", m.inner_text().strip(), m.get_attribute("aria-label"))
                
        page.screenshot(path="04_Assets/thread_menu_options.png")
        print("Saved screenshot to 04_Assets/thread_menu_options.png")

if __name__ == '__main__':
    inspect_chat_actions()
