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 click_first_artifact():
    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/library/artifacts", wait_until="domcontentloaded", timeout=30000)
        time.sleep(3)
        
        # Click the first artifact card
        # The cards in library_view.png are inside the grid
        cards = page.locator('.rounded-22, [role="article"], div[tabindex="0"]').all()
        print(f"Cards found: {len(cards)}")
        for i, c in enumerate(cards[:5]):
            print(f"Card [{i}]: {c.evaluate('el => el.className')}")
            
        if cards:
            print("Clicking first card with force=True...")
            cards[0].click(force=True)
            time.sleep(3)
            print("URL after click:", page.url)
            page.screenshot(path="04_Assets/first_artifact_detail.png")
            print("Saved screenshot to 04_Assets/first_artifact_detail.png")

if __name__ == '__main__':
    click_first_artifact()
