"""Probe Miami-Dade PA portal — map ALL extractable fields.

URL: apps.miamidadepa.gov/PropertySearch/
"""
from pathlib import Path
import time


def probe():
    from playwright.sync_api import sync_playwright

    out_dir = Path(__file__).parent.parent / "_probe_out" / "mdpa"
    out_dir.mkdir(parents=True, exist_ok=True)

    with sync_playwright() as p:
        browser = p.chromium.launch(headless=True)
        ctx = browser.new_context(
            user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/131",
        )
        page = ctx.new_page()

        print("[1] Loading landing page...")
        page.goto("https://apps.miamidadepa.gov/PropertySearch/",
                  wait_until="domcontentloaded", timeout=30000)
        time.sleep(8)
        print(f"    URL: {page.url}")
        print(f"    Title: {page.title()}")

        # Visible inputs
        print("\n[2] Visible inputs:")
        for inp in page.locator("input:visible, select:visible").all()[:25]:
            try:
                tag = inp.evaluate("el => el.tagName.toLowerCase()")
                id_ = inp.get_attribute("id") or ""
                name = inp.get_attribute("name") or ""
                type_ = inp.get_attribute("type") or ""
                placeholder = inp.get_attribute("placeholder") or ""
                aria = inp.get_attribute("aria-label") or ""
                if type_ == "hidden":
                    continue
                print(f"    <{tag}> id={id_!r} name={name!r} type={type_!r} placeholder={placeholder!r} aria={aria!r}")
            except Exception:
                pass

        # Buttons visible
        print("\n[3] Visible buttons (first 10):")
        for btn in page.locator("button:visible, a.button:visible, input[type=submit]:visible").all()[:10]:
            try:
                txt = (btn.inner_text() or btn.get_attribute("value") or "").strip()[:50]
                btn_id = btn.get_attribute("id") or ""
                print(f"    text={txt!r} id={btn_id!r}")
            except Exception:
                pass

        # Test search by a known Miami-Dade address.
        # Use a famous address: "1 NE 1 St Miami FL 33132" (Miami courthouse)
        # Or pick from the inputs we found.

        # Save full HTML
        (out_dir / "01_landing.html").write_text(page.content(), encoding="utf-8")
        page.screenshot(path=str(out_dir / "01_landing.png"), full_page=True)
        print(f"\n[4] Saved landing to {out_dir}/")

        # Check for SPA framework hints
        body_text = page.inner_text("body")[:500]
        print(f"\n[5] Body preview: {body_text[:400].encode('ascii','replace').decode('ascii')}")

        browser.close()


if __name__ == "__main__":
    probe()
