import sys, os
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from youtube_studio_manager import YouTubeStudioManager
import time

manager = YouTubeStudioManager(mode='chrome_stealth')
manager.start(headless=False)

try:
    shorts_url = 'https://studio.youtube.com/channel/UClZnv08RDTD4vgLOw1jmOuA/videos/short'
    print('Navigating to Shorts list:', shorts_url)
    manager.page.goto(shorts_url, wait_until='domcontentloaded', timeout=40000)
    time.sleep(6)
    
    # Check if there is an unsupported browser redirect button
    bypass_btn = manager.page.locator('a:has-text("CHUYỂN THẲNG ĐẾN YOUTUBE STUDIO"), a:has-text("CONTINUE TO YOUTUBE STUDIO")').first
    if bypass_btn.is_visible(timeout=3000):
        print('Clicking bypass button...')
        bypass_btn.click()
        time.sleep(6)

    # Screenshot shorts tab
    manager.capture_status('downloads/studio_shorts_tab.png')
    
    # Look for video rows
    rows = manager.page.locator('ytcp-video-row, #row-container').all()
    print(f'Found {len(rows)} rows in Shorts tab.')
    
    for i, r in enumerate(rows):
        try:
            title_el = r.locator('#video-title, #entity-table-cell').first
            title = title_el.inner_text().strip() if title_el.is_visible() else 'N/A'
            link_el = r.locator('a#thumbnail-anchor, a[href*="/video/"]').first
            link = link_el.get_attribute('href') if link_el.count() > 0 else 'N/A'
            print(f'[{i+1}] Title: {title} | Link: {link}')
        except Exception as e:
            print(f'[{i+1}] Error: {e}')
finally:
    manager.close()
