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 run():
    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='networkidle', timeout=30000)
        time.sleep(4)
        
        cards = page.locator('div[tabindex], a, div.rounded-22, [role="button"]').all()
        print('Cards count:', len(cards))
        for i, c in enumerate(cards[:20]):
            txt = c.inner_text().strip().replace('\n', ' ')
            aria = c.get_attribute('aria-label') or ''
            tag = c.evaluate('el => el.tagName')
            cls = c.get_attribute('class') or ''
            print(f'Card [{i}] tag={tag} class="{cls[:30]}" text="{txt[:60]}" aria="{aria[:60]}"')
            
        # Click the first card if exists
        first_card = page.locator('div[tabindex="0"], a[href*="artifact"]').first
        if first_card.count() > 0:
            print("Clicking first card...")
            first_card.click()
            time.sleep(3)
            page.screenshot(path="04_Assets/artifact_clicked.png")
            print("Current URL after click:", page.url)

if __name__ == '__main__':
    run()
