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:
    analytics_url = "https://studio.youtube.com/channel/UClZnv08RDTD4vgLOw1jmOuA/analytics/tab-reach_content"
    manager.page.goto(analytics_url, wait_until="domcontentloaded", timeout=45000)
    time.sleep(6)

    # Click tab "Video"
    video_chip = manager.page.locator('ytcp-chip:has-text("Video"), tp-yt-paper-tab:has-text("Video")').first
    if video_chip.is_visible(timeout=5000):
        video_chip.click()
        time.sleep(4)
        manager.capture_status("downloads/analytics_videos_tab.png")
        print("Captured Video chip.")

    # Scroll down to see traffic sources and impressions
    manager.page.evaluate("window.scrollBy(0, 500)")
    time.sleep(2)
    manager.capture_status("downloads/analytics_videos_scrolled.png")

    # Click tab "Đối tượng người xem" (Audience)
    aud_tab = manager.page.locator('tp-yt-paper-tab:has-text("Đối tượng người xem"), tp-yt-paper-tab:has-text("Audience")').first
    if aud_tab.is_visible(timeout=5000):
        aud_tab.click()
        time.sleep(4)
        manager.capture_status("downloads/analytics_audience.png")
        print("Captured Audience tab.")

finally:
    manager.close()
