"""Turn quotes of steps 2 and 3 that don't match their source into disputes for a third reader (plan section 5).

    python code/quote_disputes.py <batches dir> <records dir> <full-text dir> <out json> <reading result json> ...

Reads the reading's results as assemble.py does (later results replacing earlier ones paper by paper), checks every
step-3 quote of each paper's settled code against its abstract and full text, and writes, for reading_workflow.js's
`settle_later` mode, one dispute per paper with a quote that doesn't match: the settled code and the other reader's
code of that step, the field "quote". Step 2 has no quotes. Step 1's mismatches are listed by assemble.py and go to
the workflow's `settle` mode.
"""

import json
import sys
import unicodedata
from pathlib import Path


def norm(t: str) -> str:
    return unicodedata.normalize("NFKC", " ".join(t.split()))


def quotes(code: dict) -> list:
    out = [code.get(k) for k in ("instruments_quote", "pqtl_quote", "or_unit_quote")]
    return [q for q in out + [o.get("quote") for o in code.get("outcome_sources", [])] if q]


def main(batches: Path, records: Path, fulltext: Path, out: Path, results: list) -> None:
    batch_of = {r["pmid"]: b.stem for b in batches.glob("*.json") for r in json.loads(b.read_text())}
    settled, readers = {}, {}
    for p in results:
        r = json.loads(p.read_text())
        r = r.get("result", r)
        src = r.get("sources")
        if not isinstance(src, dict):
            continue
        for c in src.get("codes", []):
            settled[c["pmid"]] = c
        for pair in src.get("readers", []) or []:
            for k in ("a", "b"):
                for c in (pair or {}).get(k, []) or []:
                    readers.setdefault(c["pmid"], {})[k] = c
    disputes = []
    for pmid, code in sorted(settled.items()):
        rec = json.loads((records / f"{pmid}.json").read_text())
        text = norm(rec["title"] + "\n" + rec["abstract"])
        f = fulltext / f"{pmid}.txt"
        text += "\n" + (norm(f.read_text()) if f.exists() else "")
        if all(norm(q) in text for q in quotes(code)):
            continue
        other = next((c for c in readers.get(pmid, {}).values() if c != code), None)
        disputes.append({"step": "sources", "pmid": pmid, "batch": batch_of[pmid], "fields": ["quote"], "a": code, "b": other})
    out.write_text(json.dumps(disputes, ensure_ascii=False))
    print(f"{len(disputes)} step-3 codes with a quote that doesn't match: {' '.join(d['pmid'] for d in disputes)}")


if __name__ == "__main__":
    main(Path(sys.argv[1]), Path(sys.argv[2]), Path(sys.argv[3]), Path(sys.argv[4]), [Path(a) for a in sys.argv[5:]])
