"""The coded census as the bundle holds it: every code, and in place of each quote where it lies (plan section 5).

    python code/export.py <coded dir> <records dir> <full-text dir> <out dir>

Reads claims.json, diseases.json, and sources.json from <coded dir> (assemble.py's output) and writes
the same three files to <out dir> with each quote replaced by {"in": "abstract" | "full text", "start",
"end"}: character offsets into the record's title and abstract joined by one newline, or into the
full text code/fetch_fulltext.py extracts from Europe PMC (offsets.py). No abstract's or full text's
words are kept. A quote that can't be placed stops the export, since assemble.py lists such quotes as
disagreements for the third reader before this runs.
"""

import json
import sys
from pathlib import Path

from offsets import place


def main(coded: Path, records: Path, methods: Path, out: Path) -> None:
    out.mkdir(parents=True, exist_ok=True)
    abstract = {}

    def where(pmid: str, quote, allow_methods: bool):
        if not quote:
            return None
        if pmid not in abstract:
            rec = json.loads((records / f"{pmid}.json").read_text())
            abstract[pmid] = rec["title"] + "\n" + rec["abstract"]
        at = place(quote, abstract[pmid])
        if at:
            return {"in": "abstract", "start": at[0], "end": at[1]}
        m = methods / f"{pmid}.txt"
        at = place(quote, m.read_text()) if allow_methods and m.exists() else None
        if at:
            return {"in": "full text", "start": at[0], "end": at[1]}
        raise SystemExit(f"{pmid}: a quote isn't in its record; settle it before exporting")

    claims = json.loads((coded / "claims.json").read_text())
    for c in claims:
        c["eligibility_quote"] = where(c["pmid"], c.get("eligibility_quote"), False)
        if c.get("claim"):
            c["claim"]["quote"] = where(c["pmid"], c["claim"].get("quote"), False)
        for k in ("colocalisation_quote", "replication_quote", "drug_target_quote"):
            if c.get("flags") and k in c["flags"]:
                c["flags"][k] = where(c["pmid"], c["flags"][k], False)
    sources = json.loads((coded / "sources.json").read_text())
    for s in sources:
        for k in ("instruments_quote", "pqtl_quote", "or_unit_quote"):
            if k in s:
                s[k] = where(s["pmid"], s[k], True)
        for o in s["outcome_sources"]:
            o["quote"] = where(s["pmid"], o.get("quote"), True)
    (out / "claims.json").write_text(json.dumps(claims, indent=1, ensure_ascii=False) + "\n")
    (out / "diseases.json").write_text((coded / "diseases.json").read_text())
    (out / "sources.json").write_text(json.dumps(sources, indent=1, ensure_ascii=False) + "\n")
    text = (out / "claims.json").read_text() + (out / "sources.json").read_text()
    print(f"{len(claims)} papers exported, {len(text):,} characters, no quoted words")


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