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

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

try:
    edit_url = "https://studio.youtube.com/video/MtB7VLHfcDk/edit"
    manager.page.goto(edit_url, wait_until="domcontentloaded", timeout=45000)
    time.sleep(5)

    info = manager.page.evaluate("""
    () => {
        // Find scrollable
        const scrollables = Array.from(document.querySelectorAll('*')).filter(el => {
            return el.scrollHeight > el.clientHeight && el.clientHeight > 300;
        }).map(el => ({ tag: el.tagName, id: el.id, className: el.className, scrollHeight: el.scrollHeight }));

        // Find thumbnail elements
        const thumbs = Array.from(document.querySelectorAll('*')).filter(el => {
            const txt = (el.innerText || '');
            return txt.includes('Hình thu nhỏ') || txt.includes('Thumbnail');
        }).map(el => ({ tag: el.tagName, id: el.id, html: el.outerHTML.substring(0, 300) })).slice(0, 5);

        // Find related video elements
        const related = Array.from(document.querySelectorAll('*')).filter(el => {
            const txt = (el.innerText || '');
            return txt.includes('Video có liên quan') || txt.includes('Related video');
        }).map(el => ({ tag: el.tagName, id: el.id, html: el.outerHTML.substring(0, 300) })).slice(0, 5);

        return { scrollables, thumbs, related };
    }
    """)
    print("Found info:", info)

    # Now let's scroll the main scrollable element
    manager.page.evaluate("""
    () => {
        const sc = document.querySelector('#scrollable-content') || document.querySelector('ytcp-video-metadata-editor-advanced') || document.querySelector('ytcp-video-metadata-editor');
        if (sc) {
            sc.scrollTop = 500;
        } else {
            window.scrollBy(0, 500);
        }
    }
    """)
    time.sleep(2)
    manager.capture_status("downloads/studio_short_edit_scrolled_deep.png")

finally:
    manager.close()
