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 sync_worker_profile, DEFAULT_PROFILE

def test_main_chat():
    sync_worker_profile()
    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)
        
        print("Initial URL:", page.url)
        
        # Click 'Đoạn chat chính'
        main_chat = page.locator('text="Đoạn chat chính"').first
        if main_chat.count() > 0:
            print("Clicking 'Đoạn chat chính'...")
            main_chat.click()
            time.sleep(3)
            print("URL after clicking main chat:", page.url)
            page.screenshot(path="04_Assets/main_chat_screen.png")
            
            # Check connection status
            body = page.locator("body").inner_text()
            print("Has 'Đang kết nối':", "Đang kết nối" in body)
            print("Has input:", page.locator('input[type="file"]').count())
            print("Has textbox:", page.locator('textarea, [placeholder*="Nhắn tin"]').count())

if __name__ == '__main__':
    test_main_chat()
