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:
    url = "https://www.youtube.com/channel/UClZnv08RDTD4vgLOw1jmOuA/posts"
    manager.page.goto(url, wait_until="domcontentloaded", timeout=45000)
    time.sleep(6)

    box = manager.page.locator("#placeholder, yt-formatted-string:has-text('người hâm mộ'), #simplebox-placeholder, [id*='placeholder']").first
    if box.is_visible(timeout=5000):
        box.click()
        time.sleep(2)

        # Click "Hình ảnh" button
        img_btn = manager.page.locator("ytd-button-renderer:has-text('Hình ảnh'), button:has-text('Hình ảnh')").first
        print("Clicking 'Hình ảnh' button...")
        img_btn.click()
        time.sleep(3)
        manager.capture_status("downloads/community_after_img_click.png")

        # Check for file input
        file_input = manager.page.locator("input[type='file'][accept*='image']").first
        print("File input count:", file_input.count())
        if file_input.count() > 0:
            img_path = str(Path("content/thumbnails_4k/ep2_thumb_4k.jpg").resolve())
            file_input.set_input_files(img_path)
            print(f"Set image file: {img_path}")
            time.sleep(5)
            manager.capture_status("downloads/community_with_image.png")

finally:
    manager.close()
