import csv
import json
from datetime import datetime
from pathlib import Path
from playwright.sync_api import sync_playwright

OUT = Path(__file__).resolve().parent / 'output'
OUT.mkdir(exist_ok=True)

with sync_playwright() as p:
    browser = p.chromium.launch(headless=False)
    context = browser.new_context(viewport={"width": 1440, "height": 900})
    page = context.new_page()
    page.goto('https://one.dat.com/search-loads', wait_until='domcontentloaded')
    print('\nLog in manually, run a Search Loads query, and wait until results appear.')
    input('When the results are visible, press ENTER here to capture visible text... ')
    if not page.url.startswith('https://one.dat.com/'):
        raise RuntimeError('Not on DAT One. Capture canceled.')
    result = page.evaluate('''() => {
      const root = document.querySelector('dat-root') || document.body;
      const els = [...root.querySelectorAll('tr,[role="row"],article,[role="listitem"]')];
      const visible = el => { const r=el.getBoundingClientRect(); const s=getComputedStyle(el); return r.width>0 && r.height>0 && s.visibility!=="hidden" && s.display!=="none"; };
      const rows = els.filter(visible).map(el => (el.innerText||'').trim()).filter(Boolean);
      return {url: location.origin + location.pathname, headings:[...document.querySelectorAll('h1,h2,h3')].filter(visible).map(x=>x.innerText.trim()), rows:[...new Set(rows)].slice(0,100), visibleText:(root.innerText||'').slice(0,30000)};
    }''')
    stamp = datetime.now().strftime('%Y%m%d_%H%M%S')
    (OUT / f'dat_visible_{stamp}.json').write_text(json.dumps(result, indent=2, ensure_ascii=False), encoding='utf-8')
    with (OUT / f'dat_rows_{stamp}.csv').open('w', newline='', encoding='utf-8-sig') as f:
        writer = csv.writer(f)
        writer.writerow(['visible_row_text'])
        writer.writerows([[row] for row in result['rows']])
    print(f"Saved {len(result['rows'])} candidate rows and visible text to {OUT}")
    print('Review and redact any sensitive broker or account information before sharing output files.')
    browser.close()
