import sys
import time
from playwright.sync_api import sync_playwright

if hasattr(sys.stdout, 'reconfigure'):
    sys.stdout.reconfigure(encoding='utf-8')

with sync_playwright() as p:
    browser = p.chromium.connect_over_cdp("http://127.0.0.1:9224")
    page = browser.contexts[0].pages[0]

    # Đóng dialog nếu có
    page.keyboard.press("Escape")
    page.wait_for_timeout(1000)

    # Click notification bell
    notif_btn = page.locator('div[role="button"][aria-label*="Notifications"], div[role="button"][aria-label*="Thông báo"]').first
    if notif_btn.is_visible():
        notif_btn.click()
        page.wait_for_timeout(3000)
        page.screenshot(path="FacebookAutoPoster/notif_check_video.png")
        print("📸 Saved: notif_check_video.png")
        
        # In các thông báo
        dialog = page.locator("div[role='dialog']")
        if dialog.count() > 0:
            print("Notifications content:\n", dialog.inner_text()[:600])
