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)

    # Inspect the exact structure of the post box
    box_structure = manager.page.evaluate("""
    () => {
        const creator = document.querySelector('ytd-backstage-post-dialog-renderer, ytd-post-creation-renderer, ytd-create-post-renderer, ytd-comments-header-renderer, #post-creation-container');
        const allText = Array.from(document.querySelectorAll('*')).filter(el => {
            const t = el.innerText || '';
            return t.includes('Hãy đăng thông tin') || t.includes('Gửi lời cảm ơn') || t.includes('Bật mí về video');
        }).map(el => ({
            tag: el.tagName,
            id: el.id,
            className: el.className,
            text: el.innerText.substring(0, 80)
        }));
        return { allText };
    }
    """)
    print("Found text elements:", box_structure)

    # Try clicking the text element to expand
    manager.page.evaluate("""
    () => {
        const target = Array.from(document.querySelectorAll('*')).find(el => {
            const t = (el.innerText || '').trim();
            return t === 'Hãy đăng thông tin cập nhật dành cho người hâm mộ của bạn' || t === 'Gửi lời cảm ơn! Nhập @ để đề cập đến một kênh';
        });
        if (target) {
            target.click();
        }
    }
    """)
    time.sleep(3)
    manager.capture_status("downloads/community_clicked_box.png")

finally:
    manager.close()
