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]

    # Find the 3 dots button on the comment
    comment = page.locator("div:has-text('Experience the full naval warfare')").last
    comment.hover()
    time.sleep(1)

    dots = page.locator("div[aria-label='Edit or delete this'], div[aria-label*='delete this']").last
    if dots.is_visible():
        dots.click(force=True)
        time.sleep(1)

    # Click Delete
    del_btn = page.get_by_text("Delete", exact=True).first
    if del_btn.is_visible():
        del_btn.click(force=True)
        time.sleep(1.5)
        # Click the Delete button inside confirmation dialog
        confirm_btn = page.locator("div[role='dialog'] div[role='button']:has-text('Delete')").last
        if confirm_btn.is_visible():
            confirm_btn.click(force=True)
            print("Successfully deleted comment!")
            time.sleep(2)

    page.screenshot(path="FacebookAutoPoster/comment_actually_deleted.png")
    print("Saved comment_actually_deleted.png")
