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_current():
    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(3)
        
        # Attach image
        img_path = Path("04_Assets/keyframes/ep01_scene01_shot01/start_frame_720p.png").resolve()
        file_input = page.locator('input[type="file"]').first
        if file_input.count() > 0:
            print("Attaching image...")
            file_input.set_input_files(str(img_path))
            time.sleep(3)
            
        prompt = "Tạo video 10 giây điện ảnh: Tiếp diễn sau cổng thành Đại Minh Gia Tĩnh: Cú máy bay lướt êm ả dọc theo dòng sông biếc uốn lượn ngoại ô kinh thành. Mặt nước trong veo như ngọc phản chiếu rặng liễu tơ mềm và làn sương sớm mai lảng bảng. Cảnh sắc mùa xuân thanh bình, xa xa thoáng bóng thuyền nan lướt nhẹ, 24fps cinematic drama. Âm thanh: Tiếng nước sông vỗ nhẹ vào bờ đất, tiếng mái chèo khua nước êm đềm từ xa, tiếng gió xuân mát rượi lướt qua rặng liễu. Quy tắc âm thanh: Tuyệt đối KHÔNG sinh nhạc nền, không âm thanh điện tử, không tạp âm rè nhiễu. Chỉ sinh foley và ambience."
        chat_box = page.locator('textarea, [placeholder*="Nhắn tin"], [placeholder*="Message"], [role="textbox"]').first
        chat_box.click()
        time.sleep(1)
        chat_box.fill(prompt)
        time.sleep(1)
        
        send_btn = page.locator('button[data-pel-click="chat_send_message"], button[aria-label="Gửi"], button[aria-label*="Gửi"]').first
        if send_btn.count() > 0 and send_btn.is_visible():
            send_btn.click()
        else:
            chat_box.press("Enter")
            
        time.sleep(5)
        page.screenshot(path="04_Assets/send_current_thread_5s.png")
        
        # Observe for 25 seconds
        for i in range(5):
            time.sleep(5)
            print(f"[{(i+1)*5}s] Checking state...")
            
        page.screenshot(path="04_Assets/send_current_thread_30s.png")

if __name__ == '__main__':
    test_send_current()
