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')
    context = browser.contexts[0]
    page = context.pages[0] if context.pages else context.new_page()
    page.goto('https://www.facebook.com/profile.php?id=61594841053758', wait_until='domcontentloaded')
    page.wait_for_timeout(3000)
    
    # Cuộn xuống bài viết Day 1
    page.mouse.wheel(0, 800)
    page.wait_for_timeout(2000)
    page.screenshot(path='FacebookAutoPoster/day1_post_detail.png')
    
    # Kiểm tra notifications
    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/notifications_opened.png')
        notifs_text = page.locator('div[role="dialog"]').inner_text() if page.locator('div[role="dialog"]').count() > 0 else ""
        print("Notifications list:\n", notifs_text[:400])
        # Đóng dialog
        page.keyboard.press("Escape")
        page.wait_for_timeout(1000)
