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:
    studio_url = "https://studio.youtube.com/channel/UClZnv08RDTD4vgLOw1jmOuA/videos"
    print(f"Navigating to: {studio_url}")
    manager.page.goto(studio_url, wait_until="domcontentloaded", timeout=45000)
    time.sleep(6)

    # Bypass if needed
    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)

    # Click on "Bài đăng" tab
    posts_tab = manager.page.locator('tp-yt-paper-tab:has-text("Bài đăng"), tp-yt-paper-tab:has-text("Posts")').first
    print("Posts tab visible?:", posts_tab.is_visible(timeout=5000))
    if posts_tab.is_visible():
        posts_tab.click()
        time.sleep(5)
        print("URL after clicking Posts tab:", manager.page.url)
        manager.capture_status("downloads/studio_posts_clicked.png")

        # Inspect elements inside the posts tab
        btn = manager.page.locator('button:has-text("Tạo bài đăng"), ytcp-button:has-text("Tạo bài đăng"), a:has-text("Tạo bài đăng")').first
        print("Create post button visible?:", btn.is_visible(timeout=5000))

finally:
    manager.close()
