import sys
import time
from pathlib import Path

sys.path.insert(0, '05_Production_Pipeline')
if sys.platform == 'win32':
    sys.stdout.reconfigure(encoding='utf-8')
from invisible_playwright import InvisiblePlaywright

pdir = Path.home() / 'AntigravityProfiles' / 'phu2234_worker'
with InvisiblePlaywright(profile_dir=pdir, headless=True) as ctx:
    page = ctx.pages[0] if ctx.pages else ctx.new_page()
    page.goto('https://muse.ai/thread/new', wait_until='domcontentloaded', timeout=25000)
    time.sleep(4)
    
    # Tìm nút lịch sử của Shot 01 (10:03)
    target_btn = None
    for b in page.locator('button').all():
        txt = b.inner_text().strip()
        if 'làng quê 1980' in txt or '10:03' in txt:
            target_btn = b
            print('Found Shot 01 thread button:', txt.replace('\n', ' -- '))
            break
            
    if target_btn:
        target_btn.click()
        time.sleep(4)
        print('Clicked into thread! Current URL:', page.url)
        
        # Check download buttons or video tags
        dl_sel = 'button[data-pel-click="media_download"], button[aria-label*="Tải video xuống"], button[aria-label*="Download"]'
        dl_count = page.locator(dl_sel).count()
        video_count = page.locator('video').count()
        print(f'Download buttons: {dl_count} | Videos in DOM: {video_count}')
        
        if video_count > 0:
            for idx, v in enumerate(page.locator('video').all()):
                print(f'Video #{idx}: src={v.get_attribute("src")}')
