#!/usr/bin/env python3
"""CHECK for the directory-census extraction artifact.

Re-derives the census from the authoritative source repo_tree.tsv, compares it
against the committed directory-census.tsv, and checks manifest.json against the
derived figures. Exits 0 only if every check passes.
"""

import csv
import json
import os
import sys

HERE = os.path.dirname(os.path.abspath(__file__))
REPO = os.path.abspath(os.path.join(HERE, "..", "..", ".."))
SRC = os.path.join(REPO, "repo_tree.tsv")
OUT = HERE

sys.path.insert(0, HERE)
import extract_directory_census as ext  # noqa: E402

fails = []


def check(name, cond, detail=""):
    if cond:
        print("PASS " + name)
    else:
        print("FAIL " + name + " " + repr(detail))
        fails.append(name)


rows, malformed = ext.parse_source(SRC)

with open(os.path.join(OUT, "directory-census.tsv"), encoding="utf-8", newline="") as fh:
    committed = list(csv.reader(fh, delimiter="\t"))
header, committed_rows = committed[0], committed[1:]

check("tsv header", header == ["rank", "directory", "file_count"], repr(header))
check("row count", len(committed_rows) == len(rows),
      "committed=%d fresh=%d" % (len(committed_rows), len(rows)))

if len(committed_rows) == len(rows):
    mism = [i for i, (a, b) in enumerate(zip(committed_rows, rows))
            if [str(b["rank"]), b["directory"], str(b["file_count"])] != a]
    check("rows identical to fresh re-derive", not mism,
          "first mismatch idx %s" % mism[:1])

total_files = sum(r["file_count"] for r in rows)
distinct = len({r["directory"] for r in rows})
top25_files = sum(r["file_count"] for r in rows[:25])

check("total_files", total_files == 3343, "total=%d" % total_files)
check("distinct_directories", distinct == 148, "distinct=%d" % distinct)
check("malformed_rows", len(malformed) == 1, "malformed=%d" % len(malformed))
check("top_directory", rows[0]["directory"] == "wizard-work/sphinx/archive-memgallery-2026-10-21/figures"
      and rows[0]["file_count"] == 635)
check("top25_files", top25_files == 2638, "top25=%d" % top25_files)
check("malformed row is the quoted CHECK artifact",
      len(malformed) == 1 and "wizard-work/beam/CHECK" in malformed[0][2])

with open(os.path.join(OUT, "manifest.json"), encoding="utf-8") as fh:
    man = json.load(fh)

check("manifest.rows", man.get("rows") == len(rows),
      "manifest=%r actual=%d" % (man.get("rows"), len(rows)))
check("manifest.total_files", man.get("total_files") == total_files,
      "manifest=%r actual=%d" % (man.get("total_files"), total_files))
check("manifest.malformed_rows", man.get("malformed_rows") == len(malformed))

if fails:
    print("")
    print("%d CHECK(S) FAILED" % len(fails))
    sys.exit(1)

print("")
print("ALL CHECKS PASSED: %d rows, %d distinct dirs, %d total_files, %d malformed"
      % (len(rows), distinct, total_files, len(malformed)))
