"""Test interactivo de court_records.py — solo Duval.

Empieza con sitios publicos:
1. Property Appraiser (paopropertysearch.coj.net) — sin login
2. Official Records (or.duvalclerk.com) — con disclaimer

Si los selectores no matchean, dump page.content() a un archivo para
inspeccionar la estructura real y ajustar.
"""
from __future__ import annotations

import io
import os
import sys
import time
from pathlib import Path

# Stdout UTF-8 para emojis
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", errors="replace")

ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT))

# Forzar el flag para esta sesion de test
os.environ["ENABLE_COURT_RECORDS"] = "true"

from data_fetchers.court_records import fetch_court_records  # noqa: E402


def main() -> int:
    print("=" * 70)
    print("Test interactivo: court_records.py — Duval scraper")
    print("=" * 70)
    print(f"ENABLE_COURT_RECORDS: {os.getenv('ENABLE_COURT_RECORDS')}")
    print()

    test_addresses = [
        ("117 W Duval St, Jacksonville, FL 32202", "Jacksonville City Hall — confirma scraper funciona"),
        ("5005 N Pearl St, Jacksonville, FL 32206", "Pearl St real (encontrado en busqueda broad)"),
        ("3245 N Pearl St, Jacksonville, FL 32206", "Fictional address — expect no results (graceful)"),
    ]

    for addr, desc in test_addresses:
        print("─" * 70)
        print(f"TEST: {addr}")
        print(f"  ({desc})")
        print("─" * 70)
        t0 = time.perf_counter()
        result = fetch_court_records(address=addr, county_name="Duval")
        elapsed = time.perf_counter() - t0
        print(f"  status: {result.get('status')}")
        print(f"  owner_name: {result.get('owner_name')}")
        print(f"  re_number: {result.get('re_number')}")
        print(f"  lis_pendens_count: {result.get('lis_pendens_count', 0)}")
        print(f"  sources_used: {result.get('sources_used', [])}")
        print(f"  errors: {len(result.get('errors', []))} errors")
        for e in (result.get('errors') or [])[:3]:
            print(f"    - {e[:200]}")
        print(f"  elapsed: {elapsed:.1f}s")
        print()

    return 0


def main_OLD() -> int:
    address = "3245 N Pearl St, Jacksonville, FL 32206"
    print(f"Target address: {address}")
    t0 = time.perf_counter()
    result = fetch_court_records(address=address, county_name="Duval")
    elapsed = time.perf_counter() - t0

    print(f"Tiempo total: {elapsed:.1f}s")
    print()
    print("=" * 70)
    print("RESULTADO")
    print("=" * 70)
    for k, v in result.items():
        if k == "lis_pendens" and isinstance(v, list):
            print(f"  {k}: {len(v)} case(s)")
            for i, c in enumerate(v, 1):
                print(f"    [{i}] {c}")
        elif k == "errors":
            print(f"  {k}: ({len(v)} errores)")
            for e in v:
                print(f"    - {e}")
        else:
            print(f"  {k}: {v}")

    print()
    if result.get("status") == "LIS_PENDENS_ACTIVE":
        print("  → DETECCION CONFIRMADA: foreclosure case activo en Duval")
        return 0
    elif result.get("status") == "CLEAN":
        print("  → Sin lis pendens encontrado (puede ser limpio o owner_name no matchea)")
        return 0
    elif result.get("status") == "UNKNOWN":
        print("  → UNKNOWN: scraper fallo en algun paso. Ver errors arriba.")
        return 1
    else:
        print(f"  → Status: {result.get('status')}")
        return 1


if __name__ == "__main__":
    sys.exit(main())
