"""Test Miami-Dade parser filters REDEEMED/CANCELED/SOLD cases."""
from __future__ import annotations
import io, sys, glob
from pathlib import Path

sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", errors="replace")
ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT))

from scrapers.miami_dade_clerk import _parse_cases_from_html, _is_status_dead


def test_status_classifier():
    print("=" * 60)
    print("UNIT: _is_status_dead()")
    print("=" * 60)
    dead = ["Redeemed", "REDEEMED", "redeemed", "Canceled", "Cancelled",
            "Sold", "Closed", "Title Transferred", "Withdrawn", "Dismissed"]
    live = ["", "Pending", "Scheduled", "Active", "Auction", "Postponed", None]

    failures = 0
    for s in dead:
        r = _is_status_dead(s)
        status = "✅" if r else "❌"
        print(f"  {status} _is_status_dead({s!r}) = {r}")
        if not r:
            failures += 1
    for s in live:
        r = _is_status_dead(s)
        status = "✅" if not r else "❌"
        print(f"  {status} _is_status_dead({s!r}) = {r}")
        if r:
            failures += 1
    return failures


def test_parse_cached():
    print()
    print("=" * 60)
    print("PARSE: cached HTML files (real Miami-Dade data)")
    print("=" * 60)
    files = sorted(glob.glob(str(ROOT / ".cache/scrapers/miami_dade_clerk/*.html")))
    print(f"Found {len(files)} cached HTML files")

    total_cases = 0
    total_filtered = 0
    file_stats = []
    for f in files:
        html = Path(f).read_text(encoding="utf-8")
        if len(html) < 20_000:
            continue  # skip "no results" pages

        logs: list[str] = []
        cases = _parse_cases_from_html(html, log_fn=lambda m: logs.append(m))
        # Look for "filtered N dead" log
        filtered_n = 0
        for log in logs:
            import re
            m = re.search(r"filtered (\d+) dead", log)
            if m:
                filtered_n = int(m.group(1))
                break

        fname = Path(f).name[:30]
        total_cases += len(cases)
        total_filtered += filtered_n
        file_stats.append((fname, len(cases), filtered_n, logs))

    print()
    print(f"{'File':<35} {'live':>5} {'filtered_dead':>14}")
    print("-" * 60)
    for fname, n_live, n_filtered, _ in file_stats:
        print(f"{fname:<35} {n_live:>5} {n_filtered:>14}")
    print("-" * 60)
    print(f"{'TOTAL':<35} {total_cases:>5} {total_filtered:>14}")

    print()
    print(f"Total live cases parsed: {total_cases}")
    print(f"Total dead cases filtered out: {total_filtered}")

    # Inspect statuses of LIVE cases — make sure no Redeemed/etc leaked through
    print()
    print("=== STATUS audit on live cases (sample 10) ===")
    sample_count = 0
    for fname, _, _, _ in file_stats:
        if sample_count >= 10:
            break
        html = (ROOT / f".cache/scrapers/miami_dade_clerk/{fname}").read_text(encoding="utf-8") if (ROOT / f".cache/scrapers/miami_dade_clerk/{fname}").exists() else None

    # Direct: re-parse first non-empty file and show statuses
    for f in files:
        html = Path(f).read_text(encoding="utf-8")
        if len(html) < 20_000:
            continue
        cases = _parse_cases_from_html(html)
        if not cases:
            continue
        print(f"  Statuses in {Path(f).name[:30]}:")
        statuses_seen = set()
        for c in cases[:50]:
            statuses_seen.add(c.get("auction_status") or "(empty)")
        print(f"    unique statuses on live cases: {statuses_seen}")
        # Verify NO dead status leaked through
        dead_leak = [c for c in cases if _is_status_dead(c.get("auction_status"))]
        if dead_leak:
            print(f"    ❌ {len(dead_leak)} DEAD status leaked through filter!")
            return 1
        else:
            print(f"    ✅ All {len(cases)} live cases have non-dead status")
        break

    return 0


def main() -> int:
    rc = test_status_classifier()
    rc += test_parse_cached()
    print()
    print("=" * 60)
    if rc == 0:
        print("✅ ALL TESTS PASSED — REDEEMED/CANCELED filtering works")
    else:
        print(f"❌ {rc} failure(s)")
    return rc


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