import sys, time
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=60000)
    time.sleep(6)

    # 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)

    # Find all board/content links
    all_links = []
    for e in client.page.query_selector_all('a[href*="/rmacsparran/"]'):
        href = e.get_attribute('href') or ''
        txt = (e.inner_text() or '').strip()[:60]
        if href not in ('/rmacsparran/', '/rmacsparran/_created/', '/rmacsparran/_saved/') and href:
            all_links.append({'href': href, 'txt': txt})
    print(f'Profile links: {len(all_links)}')
    for b in all_links:
        print(f'  {b}')

    # Also check if "Test Delete" board URL works
    client.page.goto('https://www.pinterest.com/rmacsparran/rmpropworks-test-delete/', timeout=20000)
    time.sleep(3)
    url = client.page.url
    print(f'\nTest board URL: {url}')
    if 'rmpropworks-test-delete' in url.lower() or '404' not in client.page.content():
        print('Board EXISTS')
    else:
        print('Board does NOT exist (404)')
