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

    # Try the boards listing URL
    for url in [
        'https://www.pinterest.com/rmacsparran/boards/',
        'https://uk.pinterest.com/rmacsparran/boards/',
        'https://www.pinterest.com/rmacsparran/',
    ]:
        print(f'\nTrying: {url}')
        client.page.goto(url, timeout=30000)
        time.sleep(5)
        final_url = client.page.url
        print(f'Final URL: {final_url}')

        # Take screenshot
        fname = url.replace('https://', '').replace('/', '_').strip('_') + '.png'
        client.page.screenshot(path=str(data_dir / fname))

        # Find board elements
        boards_found = []
        for sel in ['[data-test-id="board-thumbnail"]', '[data-test-id="boardThumbnail"]',
                    '[data-test-id="board-card"]', '[data-test-id="board"]',
                    'a[href*="/rmacsparran/"][href*="/"]']:
            elems = client.page.query_selector_all(sel)
            if elems:
                for e in elems:
                    try:
                        txt = (e.inner_text() or '').strip()[:40]
                        href = e.get_attribute('href') or ''
                        if txt or href:
                            boards_found.append({'sel': sel, 'txt': txt, 'href': href[:60]})
                    except:
                        pass
        if boards_found:
            print(f'Boards found: {len(boards_found)}')
            for b in boards_found[:10]:
                print(f'  {b}')

        # Find create board button
        for sel in ['[data-test-id*="create"]', '[aria-label*="create" i]',
                    'button:has-text("Create")', '[data-test-id="boardActionsButton"]']:
            try:
                e = client.page.query_selector(sel)
                if e:
                    print(f'Create button found with: {sel!r} txt={e.inner_text()[:30]!r}')
            except:
                pass
