import time
from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.connect_over_cdp('http://localhost:9222')
    page = browser.contexts[0].pages[0]
    page.goto('https://www.facebook.com/me/about', wait_until='domcontentloaded', timeout=30000)
    page.wait_for_timeout(3000)
    
    # 1. Chi tiết về bản thân (Details about you)
    print('Updating Details about you...')
    page.locator("span:has-text('Details about you')").first.click()
    page.wait_for_timeout(2000)
    
    write_about = page.locator("text=/Write some details about yourself/i").first
    if write_about.is_visible():
        write_about.click()
        page.wait_for_timeout(1000)
        textbox = page.locator("textarea").first
        bio_text = '''Du học sinh & Content Creator tại California ☀️ Đam mê lịch sử, công nghệ và văn hóa Việt Nam. Đang cùng team phát triển dự án phim tài liệu lịch sử 4K 'Forgotten Battles' (History Vault) 🇻🇳🇺🇸'''
        textbox.fill(bio_text)
        page.wait_for_timeout(1000)
        save_btn = page.locator("div[aria-label='Save'], button:has-text('Save')").first
        if save_btn.is_visible():
            save_btn.click()
            print('Saved Details about you!')
            page.wait_for_timeout(3000)
            
    # Chụp ảnh sau khi lưu
    page.screenshot(path=r'C:\Projects\Historical\FacebookAutoPoster\about_updated.png')
    print('Updated screenshot saved!')
