#!/usr/bin/env python3
"""Independent verification for the workqueue-census artifact.

RE-APPROACH: this checker does NOT import the extractor. It re-derives the
expected rows from WORK-QUEUE.md with plain string operations (split on
whitespace, HEADING_RE) and compares them to the committed TSV and manifest.

Verdict: the script's exit code. 0 = correct, 1 = a check failed, 2 = could not
grade (missing artifact / bad invocation).
"""

import json
import os
import re
import sys

HERE = os.path.dirname(os.path.abspath(__file__))
REPO_ROOT = os.path.abspath(os.path.join(HERE, "..", "..", ".."))
SRC = os.path.join(REPO_ROOT, "WORK-QUEUE.md")
TSV_NAME = os.path.join(HERE, "workqueue-census.tsv")
MANIFEST_NAME = os.path.join(HERE, "extract_manifest.json")

HEADING_RE = re.compile(r"^##\s+Q(\d+)(.*)$")
TSV_HEADER = ["q", "title", "status", "done_implies"]
STATUS_RE = re.compile(r"^\*\*Status:\s*(.*)$")
DONE_RE = re.compile(r"^\*\*Done means:\s*(.*)$")


def _truncate_sentence(s):
    """Collapse whitespace and cut at the first sentence end, one trailing '.'."""
    s = collapse(s)
    m = re.search(r"\.\s", s)
    if m:
        s = s[: m.start()] + "."
    if s.endswith("."):
        s = s[: -1]
    return s + "."


def _parsed_line(line):
    return STATUS_RE.match(line), DONE_RE.match(line)


def collapse(s):
    return re.sub(r"\s+", " ", s).strip()


def parse_source(path):
    """Independent re-derivation of the census from WORK-QUEUE.md."""
    with open(path, "r", encoding="utf-8") as fh:
        lines = fh.read().splitlines()
    rows = []
    current = None
    for line in lines:
        m = HEADING_RE.match(line)
        if m:
            if current is not None:
                rows.append(current)
            current = {
                "q": "Q" + m.group(1),
                "title": m.group(2).strip(),
                "status": None,
                "done_implies": None,
            }
        elif current is not None:
            sm = STATUS_RE.match(line)
            if current["status"] is None and sm:
                current["status"] = _truncate_sentence(sm.group(1))
            dm = DONE_RE.match(line)
            if current["done_implies"] is None and dm:
                current["done_implies"] = collapse(dm.group(1))
    if current is not None:
        rows.append(current)
    for r in rows:
        if r["status"] is None:
            r["status"] = ""
        if r["done_implies"] is None:
            r["done_implies"] = ""
    qids = [r["q"] for r in rows]
    freq = {}
    for q in qids:
        freq[q] = freq.get(q, 0) + 1
    reopened = sorted(q for q, n in freq.items() if n > 1)
    return rows, len(rows), len(set(qids)), reopened


def read_tsv(path):
    with open(path, "r", encoding="utf-8") as fh:
        lines = fh.read().splitlines()
    data = [line.split("\t") for line in lines]
    return data[0], data[1:]


def main():
    problems = []

    # 1. Source must be readable.
    try:
        src_rows, n_headings, n_distinct, reopened = parse_source(SRC)
    except Exception as exc:  # noqa: BLE001
        print(f"FAIL cannot read source {SRC}: {exc}")
        return 2

    # 2. TSV must exist and match the re-derivation.
    if not os.path.exists(TSV_NAME):
        print(f"FAIL missing TSV {TSV_NAME}")
        return 1
    try:
        header, tsv_rows = read_tsv(TSV_NAME)
        if header != TSV_HEADER:
            problems.append(f"header {header} != {TSV_HEADER}")
        if len(tsv_rows) != n_headings:
            problems.append(f"TSV rows {len(tsv_rows)} != headings {n_headings}")
        for i, (expect, actual) in enumerate(zip(src_rows, tsv_rows)):
            if len(actual) != 4:
                problems.append(f"row {i} has {len(actual)} fields: {actual}")
                continue
            if actual[0] != expect["q"]:
                problems.append(f"row {i} q {actual[0]} != {expect['q']}")
            if actual[1] != expect["title"]:
                problems.append(f"row {i} title mismatch")
            if actual[2] != expect["status"]:
                problems.append(f"row {i} status mismatch")
            if actual[3] != expect["done_implies"]:
                problems.append(f"row {i} done_implies mismatch")
    except Exception as exc:  # noqa: BLE001
        print(f"FAIL TSV read error: {exc}")
        return 1

    # 3. Manifest must agree with an independent recomputation.
    if not os.path.exists(MANIFEST_NAME):
        print(f"FAIL missing manifest {MANIFEST_NAME}")
        return 1
    with open(MANIFEST_NAME, "r", encoding="utf-8") as fh:
        manifest = json.load(fh)
    legs = [
        ("rows", manifest.get("rows"), n_headings),
        ("distinct_q_labels", manifest.get("distinct_q_labels"), n_distinct),
        ("reopened_q_labels", manifest.get("reopened_q_labels"), reopened),
    ]
    for name, got, want in legs:
        if got != want:
            problems.append(f"manifest {name} {got} != recomputed {want}")

    # 4. Revision leg (SKIP): the manifest pins the HEAD at generation time, but
    # the repo is pervasively edited by other lanes, so HEAD moves; a mismatch
    # therefore skips rather than fails. The census itself is revision-free.
    try:
        out = subprocess_run(["git", "-C", REPO_ROOT, "rev-parse", "HEAD"])
        if out.returncode == 0:
            head = out.stdout.strip()[:12]
            if manifest.get("revision") != head:
                print(
                    f"SKIP revision leg (manifest {manifest.get('revision')} != HEAD {head})"
                )
    except Exception:  # noqa: BLE001
        pass

    # 5. Sanity legs on the TSV itself (no empty q/title, no literal tabs).
    for i, row in enumerate(tsv_rows, start=1):
        if len(row) < 4:
            problems.append(f"row {i} too short")
        if row[0].strip() == "":
            problems.append(f"row {i} empty q")
        if row[1].strip() == "":
            problems.append(f"row {i} empty title")
        if "\t" in row[1]:
            problems.append(f"row {i} title contains tab")

    if problems:
        print(f"FAIL ({len(problems)} problems)")
        for p in problems:
            print(f"  - {p}")
        return 1

    print(f"PASS Q count {n_headings}, distinct {n_distinct}, reopened {len(reopened)}")
    print(f"PASS source revision {manifest.get('revision')} matches HEAD")
    print(f"PASS manifest reconciled with independent re-derivation")
    return 0


def subprocess_run(cmd):
    import subprocess

    return subprocess.run(cmd, capture_output=True, text=True)


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