import sys
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&sk=directory_work'
    page.goto(url, wait_until='domcontentloaded', timeout=30000)
    page.wait_for_timeout(3000)
    
    # 1. Open Work experience form
    comp_input = page.locator("input[aria-label='Company']")
    if comp_input.count() == 0:
        print('Clicking Work experience button...')
        page.locator("div[role='button']:has-text('Work experience'), span:has-text('Work experience')").first.click()
        page.wait_for_timeout(2000)
        comp_input = page.locator("input[aria-label='Company']")
        
    print('Typing Company: History Vault...')
    comp_input.first.click()
    comp_input.first.fill('History Vault')
    page.wait_for_timeout(2000)
    
    # Check for dropdown item
    options = page.locator("div[role='listbox'] div[role='option'], div[role='option'], ul[role='listbox'] li")
    print('Company dropdown options found:', options.count())
    if options.count() > 0:
        print('Selecting first company option:', options.first.inner_text().replace('\n', ' '))
        options.first.click()
    else:
        page.keyboard.press('Enter')
    page.wait_for_timeout(2000)
    
    # 2. Position
    print('Typing Position: Founder & Video Producer...')
    pos_input = page.locator("input[aria-label='Position']").first
    pos_input.click()
    pos_input.fill('Founder & Video Producer')
    page.wait_for_timeout(1000)
    # If dropdown appears, select or press enter/escape
    pos_opts = page.locator("div[role='listbox'] div[role='option'], div[role='option']")
    if pos_opts.count() > 0:
        page.keyboard.press('Escape')
    else:
        page.keyboard.press('Tab')
    page.wait_for_timeout(1000)
    
    # 3. City/Town
    print('Typing City/Town: San Jose, California...')
    city_input = page.locator("input[aria-label='City/Town']").first
    city_input.click()
    city_input.fill('San Jose, California')
    page.wait_for_timeout(2000)
    city_opts = page.locator("div[role='listbox'] div[role='option'], div[role='option']")
    print('City dropdown options:', city_opts.count())
    if city_opts.count() > 0:
        city_opts.first.click()
    else:
        page.keyboard.press('Enter')
    page.wait_for_timeout(1000)
    
    # 4. Description
    print('Typing Description...')
    desc = page.locator("textarea")
    if desc.count() > 0:
        desc.first.fill('Producing high-impact 4K historical animation documentaries.')
    page.wait_for_timeout(1000)
    
    page.screenshot(path=r'C:\Projects\Historical\FacebookAutoPoster\work_ready_to_save.png')
    
    # 5. Click Save
    print('Clicking Save...')
    save_btn = page.locator("div[role='button']:has-text('Save'), span:has-text('Save')").first
    save_btn.click()
    page.wait_for_timeout(5000)
    
    page.screenshot(path=r'C:\Projects\Historical\FacebookAutoPoster\work_final_saved.png')
    print('Finished submit_work_robust.py!')
