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 Created tab
    action_bar = client.page.query_selector('[data-test-id="action-bar"]')
    if action_bar:
        for l in action_bar.query_selector_all('a, button, [role="tab"]'):
            if 'Created' in (l.inner_text() or ''):
                l.click()
                break
    time.sleep(5)
    client.page.evaluate('window.scrollTo(0, 500)')
    time.sleep(2)

    # Force click the Create button
    create_btn = client.page.query_selector('[data-test-id="boardActionsButton"]')
    if not create_btn:
        print('boardActionsButton NOT found')
        sys.exit(1)

    create_btn.click(force=True)
    time.sleep(4)
    print('URL:', client.page.url)

    # Dump ALL elements via JS (doesn't block like screenshot)
    elements = client.page.evaluate('''() => {
        const results = [];
        const all = document.querySelectorAll('[data-test-id], input, textarea, [role="dialog"], [role="menu"], [aria-modal]');
        const seen = new Set();
        for (const el of all) {
            const testid = el.getAttribute('data-test-id') || '';
            const role = el.getAttribute('role') || '';
            const ariaModal = el.getAttribute('aria-modal') || '';
            const tag = el.tagName;
            const txt = (el.textContent || '').trim().substring(0, 80);
            const placeholder = el.getAttribute('placeholder') || '';
            const type = el.getAttribute('type') || '';
            const key = testid + '|' + role + '|' + tag + '|' + txt.substring(0, 20);
            if (!seen.has(key) && (testid || role || ariaModal || tag === 'INPUT' || tag === 'TEXTAREA')) {
                seen.add(key);
                results.push({tag, testid, role, ariaModal, txt, placeholder, type});
            }
        }
        return results;
    }''')

    print(f'Found {len(elements)} elements')
    # Show only interesting ones
    skip_testids = {'hiddenSkipToContentContainer', 'header', 'header-PartnerHeader',
                    'pro-partner-header', 'nags-container', 'dynamic-menu-controller',
                    'mega-nav-header-name', 'business-account-switcher', 'gestalt-avatar-svg',
                    'search-box-container', 'bell-icon', 'notifications-button', 'engagement-tab',
                    'header-profile', 'header-accounts-options-button', 'floating-footer',
                    'footer-more-button-container', 'share-profile-auth', 'profile-cover-edit-button-sticky-bar',
                    'profile-header', 'profile-name', 'profile-username', 'profile-followers-count',
                    'profile-following-count', 'main-user-description-text', 'website-icon-and-url',
                    'edit-button', 'business-profile-header-cover-container', 'profile-cover-edit-button',
                    'action-bar', 'footer', 'resource-pending-fetch-script', 'resource-response-data',
                    'resource-response-script', 'nags-container'}

    for e in elements:
        if e['testid'] not in skip_testids:
            print(f"  {e['tag']} testid={e['testid']!r} role={e['role']!r} ariaModal={e['ariaModal']!r} placeholder={e['placeholder']!r} txt={e['txt'][:50]!r}")
