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"
    print(f"Navigating to edit URL: {edit_url}")
    manager.page.goto(edit_url, wait_until="domcontentloaded", timeout=45000)
    time.sleep(6)

    # Check for bypass button if any
    bypass = manager.page.locator('a:has-text("CHUYỂN THẲNG ĐẾN YOUTUBE STUDIO"), a:has-text("CONTINUE TO YOUTUBE STUDIO")').first
    if bypass.is_visible(timeout=3000):
        bypass.click()
        time.sleep(6)

    manager.capture_status("downloads/studio_short_edit_page.png")
    
    # Check title field
    title_box = manager.page.locator("#textbox[aria-label*='tiêu đề'], #textbox[aria-label*='title'], #title-textarea #textbox").first
    print("Title box visible:", title_box.is_visible(timeout=5000))
    if title_box.is_visible():
        print("Current title:", title_box.inner_text())

    # Check thumbnail section
    thumb_btn = manager.page.locator("button#upload-button, ytcp-button#upload-button, input#file-loader").first
    print("Thumb input/button count:", thumb_btn.count())

    # Check for any message about shorts thumbnail
    body_text = manager.page.locator("body").inner_text()
    if "hình thu nhỏ" in body_text.lower() or "thumbnail" in body_text.lower():
        print("Thumbnail keyword found in page text.")
    
    # Check related video element
    related_vid = manager.page.locator("#related-video, [aria-label*='liên quan'], [aria-label*='Related']").first
    print("Related video count:", related_vid.count())
    
finally:
    manager.close()
