import sys
import codecs
sys.stdout.reconfigure(encoding='utf-8')

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.connect_over_cdp('http://localhost:9222')
    context = browser.contexts[0]
    page = context.pages[0] if context.pages else context.new_page()
    
    url = 'https://www.facebook.com/profile.php?id=61594841053758'
    print('Navigating to profile root...')
    page.goto(url, wait_until='domcontentloaded', timeout=30000)
    page.wait_for_timeout(4000)
    
    # Click "Edit profile"
    edit_profile_btn = page.locator("div[role='button']:has-text('Edit profile'), span:has-text('Edit profile')").first
    if edit_profile_btn.count() > 0:
        print('Clicking Edit profile button...')
        edit_profile_btn.click()
        page.wait_for_timeout(3000)
        
        # Check dialog
        dialog = page.locator("div[role='dialog']")
        if dialog.count() > 0:
            print('=== DIALOG CONTENT ===')
            print(dialog.inner_text())
            page.screenshot(path=r'C:\Projects\Historical\FacebookAutoPoster\edit_profile_dialog.png')
            print('Saved edit_profile_dialog.png')
        else:
            print('Dialog not found after click')
    else:
        print('Edit profile button not found')
