import sys, time, json
from pathlib import Path
sys.path.insert(0, '/app')
from pinterest.client import PinterestClient

data_dir = Path('/app/data/pinterest')
with PinterestClient(data_dir) as client:
    client.is_authenticated()
    client.page.goto('https://www.pinterest.com/rmacsparran/_created/', timeout=30000)
    time.sleep(4)

    # Click the Created tab explicitly
    try:
        action_bar = client.page.query_selector('[data-test-id="action-bar"]')
        if action_bar:
            links = action_bar.query_selector_all('a, button, div[role="tab"]')
            for l in links:
                if 'Created' in (l.inner_text() or ''):
                    l.click()
                    print('Clicked Created tab')
                    break
    except Exception as ex:
        print('Tab click error:', ex)

    time.sleep(6)

    # Scroll to trigger lazy loading
    client.page.evaluate('window.scrollTo(0, 400)')
    time.sleep(3)

    client.page.screenshot(path=str(data_dir / 'debug_interact_boards.png'))

    # Look for boards
    all_elems = client.page.query_selector_all('[data-test-id]')
    print('All testids found:')
    seen = set()
    for e in all_elems:
        try:
            testid = e.get_attribute('data-test-id') or ''
            txt = (e.inner_text() or '').strip()[:80]
            key = testid + '|' + txt[:20]
            if key not in seen and testid:
                seen.add(key)
                print(f'  testid={testid!r} txt={txt!r}')
        except:
            pass

    # Specifically look for the + create button
    print('\nLooking for create button...')
    for sel in [
        '[aria-label="Create"]',
        '[aria-label*="create" i]',
        '[aria-label*="board" i]',
        'button[data-test-id*="create"]',
        '[data-test-id*="create"]',
        '[data-test-id*="board"]',
    ]:
        elems = client.page.query_selector_all(sel)
        if elems:
            for e in elems:
                print(f'  selector={sel!r} txt={e.inner_text()[:40]!r} aria={e.get_attribute("aria-label")!r} testid={e.get_attribute("data-test-id")!r}')

    # Try clicking the + button by coordinates (top right area)
    print('\nTrying + button click at (1220, 120)...')
    try:
        client.page.mouse.click(1220, 120)
        time.sleep(3)
        client.page.screenshot(path=str(data_dir / 'debug_after_plus_click.png'))

        # What appeared?
        new_elems = client.page.query_selector_all('[data-test-id]')
        new_seen = set()
        for e in new_elems:
            try:
                testid = e.get_attribute('data-test-id') or ''
                txt = (e.inner_text() or '').strip()[:60]
                key = testid + '|' + txt[:20]
                if key not in seen and key not in new_seen and testid:
                    new_seen.add(key)
                    print(f'  NEW: testid={testid!r} txt={txt!r}')
            except:
                pass
    except Exception as ex:
        print('Plus click error:', ex)
