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_artifacts():
    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/artifacts", wait_until="domcontentloaded", timeout=30000)
        time.sleep(4)
        
        screenshot_path = Path("04_Assets/artifacts_list.png").resolve()
        page.screenshot(path=str(screenshot_path))
        print("Screenshot saved to:", screenshot_path)
        
        # Check all cards or items in artifacts
        items = page.locator('[role="article"], .artifact, [data-testid*="artifact"], a, button').all()
        print(f"Total elements: {len(items)}")
        for i, el in enumerate(items[:30]):
            txt = el.inner_text().strip().replace("\n", " ")
            aria = el.get_attribute("aria-label") or ""
            if any(k in f"{txt} {aria}".lower() for k in ["video", "shot", "thúy", "tạo", "phút", "giây"]):
                print(f"[{i}] text='{txt}' | aria='{aria}'")

if __name__ == "__main__":
    check_artifacts()
