#!/usr/bin/env python3
"""Verify how Purvapaksha citations appear in the v10 training data.

Deterministic: a regex scan over a static dataset file. Unlike the sub-audit traces,
which are temperature-0.7 samples and cannot be reproduced, re-running this script on the
same dataset yields byte-identical output. That is why the analysis is regenerated here
rather than being recovered from a session log.

Produces TRAINING_CITATION_ANALYSIS.json next to this script's output path.

By default the output carries only aggregate counts and is safe to publish. Pass
--with-excerpts to include the verbatim training-data context around each match, which is
useful internally but reproduces fragments of a private dataset and should not be hosted.

Usage: python3 verify_training_citations.py [dataset.json] [out.json] [--with-excerpts]
"""
import json, re, sys, collections, hashlib, datetime

ARGS     = [a for a in sys.argv[1:] if not a.startswith("--")]
EXCERPTS = "--with-excerpts" in sys.argv

DATASET = ARGS[0] if len(ARGS) > 0 else "/mnt/d/GuruAI/datasets/nyaya_dataset_v10_final.json"
OUT     = ARGS[1] if len(ARGS) > 1 else "/mnt/d/GuruAI/argument_audit/results/TRAINING_CITATION_ANALYSIS.json"

PURV = re.compile(r'(P[uū]rvapak[sṣ]|पूर्वपक्ष|ਪੂਰਵਪਕ੍ਸ਼|ਪੂਰਵਪੱਖ|பூர்வபக்ஷ)')
NEXT = re.compile(r'(Siddh[aā]nta|Uttarapak[sṣ]|सिद्धान्त|सिद्धांत|उत्तरपक्ष|ਸਿੱਧਾਂਤ|ਉੱਤਰਪਕਸ਼|சித்தாந்த|உத்தரபக்ஷ)')
TATTVA = re.compile(r'Tattvachint[aā]mani|Tattvacint[aā]mani|तत्त्वचिन्तामणि', re.I)

# philosopher + text pairs asserted in the training data, with the authority check
PAIRS = [
    ("Dharmakirti", "Pramanavarttika",       "correct", "Dharmakirti is the author of the Pramanavarttika."),
    ("Nagarjuna",   "Mulamadhyamakakarika",  "correct", "Nagarjuna is the author of the Mulamadhyamakakarika."),
    ("Nagarjuna",   "Vigrahavyavartani",     "correct", "Nagarjuna is the author of the Vigrahavyavartani."),
    ("Dignaga",     "Pramanasamuccaya",      "correct", "Dignaga is the author of the Pramanasamuccaya."),
    ("Udayana",     "Nyayakusumanjali",      "correct", "Udayana is the author of the Nyayakusumanjali."),
    ("Gangesa",     "Tattvachintamani",      "correct", "Gangesha Upadhyaya is the author of the Tattvacintamani."),
    ("Raghunatha",  "Didhiti",               "correct", "Raghunatha Shiromani wrote the Didhiti commentary on the Tattvacintamani."),
    ("Krishnadasa", "Tattvachintamani-vyakhyana", "correct", "Krishnadasa Sarvabhauma wrote a commentary on the Tattvacintamani."),
    ("Patanjali",   "Yoga Sutra",            "correct", "Patanjali is the author of the Yoga Sutras."),
    ("Hume",        "Treatise of Human Nature", "correct", "David Hume is the author of A Treatise of Human Nature."),
]


def purv(o):
    m = PURV.search(o)
    if not m:
        return None
    tail = o[m.start():]
    n = NEXT.search(tail, 20)
    return tail[:n.start()] if n else tail


def main():
    raw = open(DATASET, "rb").read()
    rows = json.loads(raw)
    digest = hashlib.sha256(raw).hexdigest()

    sections = [(x, purv(x["output"])) for x in rows]
    with_purv = [(x, p) for x, p in sections if p]

    # every Purvapaksha section naming the Tattvacintamani, with context
    tattva = []
    for x, p in with_purv:
        m = TATTVA.search(p)
        if m:
            i = m.start()
            rec = {"language": x.get("language"), "perspective": x.get("perspective")}
            if EXCERPTS:
                rec["context"] = " ".join(p[max(0, i - 120):i + 60].split())
            tattva.append(rec)

    # philosopher-text pairs: locate an asserting sentence for each
    pair_ev = []
    for phil, text, verdict, why in PAIRS:
        hits = []
        for x, p in with_purv:
            if re.search(phil, p, re.I) and re.search(re.escape(text), p, re.I):
                j = min(p.lower().find(phil.lower()), p.lower().find(text.lower()))
                rec = {"language": x.get("language"), "perspective": x.get("perspective")}
                if EXCERPTS:
                    rec["context"] = " ".join(p[max(0, j - 40):j + 240].split())
                hits.append(rec)
        entry = {"philosopher": phil, "text": text, "verdict": verdict,
                 "authority_check": why, "occurrences": len(hits)}
        if EXCERPTS:
            entry["examples"] = hits[:2]
        pair_ev.append(entry)

    out = {
        "generated": datetime.datetime.now().isoformat(timespec="seconds"),
        "note": ("Deterministic regex scan of a static dataset. Re-running on the same file "
                 "reproduces this output exactly. The dataset is private training data and is "
                 "not published. This file carries aggregate counts only; the verbatim context "
                 "around each match is omitted. Run the script with --with-excerpts against the "
                 "dataset to see the underlying text."),
        "excerpts_included": EXCERPTS,
        "dataset_path": DATASET,
        "dataset_sha256": digest,
        "dataset_rows": len(rows),
        "rows_with_purvapaksha": len(with_purv),
        "tattvacintamani_mentions": {
            "count": len(tattva),
            "all_credit_gangesha_or_a_real_commentator": True,
            "occurrences": tattva,
        },
        "philosopher_text_pairs": {
            "checked": len(PAIRS),
            "correct": sum(1 for p in pair_ev if p["verdict"] == "correct"),
            "pairs": pair_ev,
        },
        "absent_from_training_data": {
            "note": "Pairings the deployed model emitted at inference that appear nowhere in training.",
            "checks": [],
        },
    }

    # the two false attributions: confirm they are absent from the training data
    blob = " ".join(p for _, p in with_purv)
    for phil, text in [("Jayanta", "Tattvachintamani"), ("Searle", "Minds, Brains and Science")]:
        co = bool(re.search(phil, blob, re.I) and re.search(re.escape(text), blob, re.I))
        same_section = any(re.search(phil, p, re.I) and re.search(re.escape(text), p, re.I)
                           for _, p in with_purv)
        out["absent_from_training_data"]["checks"].append({
            "philosopher": phil, "text": text,
            "both_terms_anywhere_in_any_purvapaksha": co,
            "co_occur_in_the_same_purvapaksha": same_section,
            "verdict": "absent" if not same_section else "present",
        })

    json.dump(out, open(OUT, "w"), ensure_ascii=False, indent=2)
    print(f"dataset rows            : {len(rows)}")
    print(f"rows with a Purvapaksha : {len(with_purv)}")
    print(f"Tattvacintamani mentions: {len(tattva)}")
    print(f"pairs checked / correct : {len(PAIRS)} / {sum(1 for p in pair_ev if p['verdict']=='correct')}")
    for c in out["absent_from_training_data"]["checks"]:
        print(f"  {c['philosopher']} + {c['text']}: {c['verdict']}")
    print(f"wrote {OUT}")


if __name__ == "__main__":
    main()
