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 check_account():
    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(3)
        
        # Check buttons and clickable items in left sidebar / top nav
        print("--- ALL BUTTONS / NAV ITEMS ---")
        buttons = page.locator("button, a")
        for i in range(buttons.count()):
            b = buttons.nth(i)
            aria = b.get_attribute("aria-label") or ""
            text = b.inner_text().strip().replace("\n", " ")
            title = b.get_attribute("title") or ""
            href = b.get_attribute("href") or ""
            if any(k in f"{aria} {text} {title} {href}".lower() for k in ["tài khoản", "account", "profile", "nâng cấp", "upgrade", "cài đặt", "settings", "plan", "gói", "credit", "lượt", "mới", "thread"]):
                print(f"[{i}] text='{text}' | aria='{aria}' | title='{title}' | href='{href}'")

        # Let's inspect the artifacts or media page
        page.goto("https://muse.ai/artifacts", wait_until="domcontentloaded", timeout=30000)
        time.sleep(3)
        print("\n--- ARTIFACTS PAGE URL & TITLE ---")
        print("URL:", page.url)
        print("Title:", page.title())
        body = page.locator("body").inner_text()
        for line in body.split("\n"):
            line_str = line.strip()
            if any(k in line_str.lower() for k in ["video", "ảnh", "lỗi", "error", "hết", "quota", "credit"]):
                print("ARTIFACTS:", line_str)

if __name__ == "__main__":
    check_account()
