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]

    # Accept Hà Quang right on sidebar if visible
    ha_quang = page.locator("span:has-text('Hà Quang')").first
    if ha_quang.is_visible():
        parent = ha_quang.locator("xpath=ancestor::div[contains(@class, 'x1qjc9v5') or contains(@class, 'x1n2onr6')][.//div[@aria-label='Confirm']]").last
        if parent.count() > 0:
            c_btn = parent.locator("div[aria-label='Confirm']").first
            if c_btn.is_visible():
                c_btn.click(force=True)
                print("✅ Đã bấm Confirm cho Hà Quang (1 mutual friend) trên sidebar!")
                time.sleep(2)

    # Click bell
    bell = page.locator("div[aria-label*='Notifications'], div[aria-label*='Thông báo']").first
    if bell.is_visible():
        bell.click(force=True)
        time.sleep(2)
        page.screenshot(path="FacebookAutoPoster/notifs_popup.png")
        print("📸 Captured notifs_popup.png")
        bell.click(force=True) # close
        time.sleep(1)

    # Click messenger
    msg_btn = page.locator("div[aria-label*='Messenger']").first
    if msg_btn.is_visible():
        msg_btn.click(force=True)
        time.sleep(2)
        page.screenshot(path="FacebookAutoPoster/messenger_popup.png")
        print("📸 Captured messenger_popup.png")
        msg_btn.click(force=True) # close
        time.sleep(1)

    print("Hoàn thành kiểm tra!")
