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_send_click():
    with InvisiblePlaywright(profile_dir=DEFAULT_PROFILE, headless=True) as ctx:
        page = ctx.pages[0] if ctx.pages else ctx.new_page()
        page.on("request", lambda r: print(f"[REQ] {r.method} {r.url}"))
        page.on("response", lambda r: print(f"[RESP {r.status}] {r.url}") if any(k in r.url for k in ["chat", "thread", "message", "api", "hatch"]) else None)
        
        page.goto("https://muse.ai", wait_until="domcontentloaded", timeout=30000)
        time.sleep(3)
        
        textarea = page.locator('textarea[placeholder*="Nhắn tin"]').first
        textarea.click()
        time.sleep(1)
        
        # Type with keyboard delay so React captures it
        textarea.press_sequentially("Xin chào Muse, bạn có khỏe không?", delay=15)
        time.sleep(2)
        
        send_btn = page.locator('button[data-pel-click="chat_send_message"]').first
        print("Send button visible:", send_btn.is_visible())
        print("Send button enabled:", send_btn.is_enabled())
        
        print("Clicking Send button...")
        send_btn.click()
        time.sleep(5)
        
        page.screenshot(path="04_Assets/click_send_5s.png")
        print("Content after 5s:", page.locator('.chat-message, [role="article"]').all_inner_texts() if page.locator('.chat-message, [role="article"]').count() > 0 else "No messages")
        
        for i in range(4):
            time.sleep(5)
            print(f"[{ (i+2)*5 }s] Checking...")
            
        page.screenshot(path="04_Assets/click_send_25s.png")

if __name__ == '__main__':
    test_send_click()
