import sys
import 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():
    sync_worker_profile()
    pdir = DEFAULT_PROFILE
    print(f"Connecting to Muse.ai with profile: {pdir}...")
    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", wait_until="domcontentloaded", timeout=30000)
        time.sleep(5)
        
        # Screenshot
        screenshot_path = Path("04_Assets/muse_current_state.png").resolve()
        page.screenshot(path=str(screenshot_path))
        print(f"Screenshot saved to: {screenshot_path}")
        
        # Check download buttons
        dl_buttons = page.locator('button:has-text("Tải video xuống"), button:has-text("Download")')
        print(f"Download buttons count: {dl_buttons.count()}")
        
        # Check stop buttons / generating
        stop_buttons = page.locator('button:has-text("Dừng"), button:has-text("Stop")')
        print(f"Stop/Cancel buttons count: {stop_buttons.count()}")
        
        # Check text in main chat container
        body_text = page.locator("body").inner_text()
        print("\n--- BODY TEXT SAMPLE (FIRST 1500 CHARS) ---")
        print(body_text[:1500])
        print("\n--- LAST 1500 CHARS ---")
        print(body_text[-1500:])

if __name__ == "__main__":
    inspect()
