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 check_library():
    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)
        
        # Click 'Thư viện'
        lib_btn = page.locator('[aria-label="Thư viện"]').first
        if lib_btn.count() == 0:
            lib_btn = page.get_by_text("Thư viện").first
            
        if lib_btn.count() > 0:
            print("Clicking 'Thư viện'...")
            lib_btn.click()
            time.sleep(4)
            page.screenshot(path="04_Assets/library_view.png")
            print("Current URL:", page.url)
            print("Saved screenshot to 04_Assets/library_view.png")
            
            # Check video or media items
            videos = page.locator('video').all()
            print(f"Videos in library: {len(videos)}")
            for i, v in enumerate(videos):
                print(f"Video [{i}]: src={v.get_attribute('src')}")
                
            dl_btns = page.locator('button[aria-label*="Tải"], [data-pel-click*="download"]').all()
            print(f"Download buttons in library: {len(dl_btns)}")
            for b in dl_btns:
                print("DL btn:", b.get_attribute("aria-label"), b.inner_text().strip())

if __name__ == '__main__':
    check_library()
