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 test_falco():
    with InvisiblePlaywright(profile_dir=DEFAULT_PROFILE, headless=True) as ctx:
        page = ctx.pages[0] if ctx.pages else ctx.new_page()
        
        async def on_resp(res):
            if "falco" in res.url or "chat" in res.url or "message" in res.url or "hatch" in res.url:
                try:
                    body = await res.text()
                    print(f"\n[RESP {res.status}] {res.url}\nBODY: {body[:500]}\n")
                except Exception as e:
                    print(f"[RESP {res.status}] {res.url} (could not read body: {e})")
        page.on("response", on_resp)
        
        page.goto("https://muse.ai", wait_until="domcontentloaded", timeout=30000)
        time.sleep(3)
        
        # New chat
        btn = page.locator('button[aria-label="Đoạn chat phụ mới"]').first
        btn.click()
        time.sleep(4)
        
        chat_box = page.locator('textarea, [placeholder*="Nhắn tin"]').first
        chat_box.click()
        time.sleep(1)
        chat_box.fill("Tạo video 10s: Cảnh sông nước mùa xuân êm đềm.")
        time.sleep(1)
        
        send_btn = page.locator('button[data-pel-click="chat_send_message"]').first
        send_btn.click()
        time.sleep(15)

if __name__ == '__main__':
    test_falco()
