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)

    # Scroll down to see full page
    manager.page.evaluate("window.scrollBy(0, 400)")
    time.sleep(2)
    manager.capture_status("downloads/studio_short_edit_scrolled.png")

    # Inspect thumbnail elements
    thumb_info = manager.page.evaluate("""
    () => {
        const results = [];
        const thumbSection = document.querySelector('ytcp-thumbnails-compact-editor, #thumbnail, #thumbnails-editor');
        if (thumbSection) {
            results.push({ tag: thumbSection.tagName, html: thumbSection.outerHTML.substring(0, 500) });
        }
        const fileInputs = Array.from(document.querySelectorAll("input[type='file']")).map(inp => ({
            id: inp.id,
            accept: inp.accept,
            visible: inp.offsetParent !== null
        }));
        return { results, fileInputs };
    }
    """)
    print("Thumb info:", thumb_info)

    # Inspect Related Video picker
    related_info = manager.page.evaluate("""
    () => {
        // Find element with text "Video có liên quan" or "Related video"
        const els = Array.from(document.querySelectorAll('*')).filter(el => 
            el.children.length === 0 && (el.innerText.includes('Video có liên quan') || el.innerText.includes('Related video'))
        );
        return els.map(el => ({
            tag: el.tagName,
            parentTag: el.parentElement ? el.parentElement.tagName : null,
            parentId: el.parentElement ? el.parentElement.id : null,
            parentClass: el.parentElement ? el.parentElement.className : null,
            parentHtml: el.parentElement ? el.parentElement.outerHTML.substring(0, 400) : null
        }));
    }
    """)
    print("Related video elements:", related_info)

finally:
    manager.close()
