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 click_second_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(4)
        
        thread_btns = page.locator('button:has-text("Tạo video"), div[role="button"]:has-text("Tạo video")')
        count = thread_btns.count()
        print(f"Found {count} thread buttons.")
        if count >= 2:
            print("Clicking thread 1 (Tạo video Thuy Kieu hoàng hôn)...")
            thread_btns.nth(1).click()
            time.sleep(4)
            page.screenshot(path="04_Assets/thread_hoang_hon_view.png")
            print("URL:", page.url)
            
            # Check for download button or video
            dl_btns = page.locator('button[aria-label*="Tải video xuống"], button[data-pel="media_download"]')
            print(f"Download buttons: {dl_btns.count()}")
            
            videos = page.locator('video')
            print(f"Videos: {videos.count()}")
            for vi in range(videos.count()):
                v = videos.nth(vi)
                print(f"  v {vi}: src={v.get_attribute('src')}")
                
            # Print page text
            text = page.locator('main').inner_text() if page.locator('main').count() > 0 else page.locator('body').inner_text()
            print("Page content excerpt:")
            for line in text.splitlines()[-20:]:
                if line.strip():
                    print("  ", line.strip())

if __name__ == '__main__':
    click_second_thread()
