#!/usr/bin/env python3
"""falsify.py -- try to break a pathway-evidence claim graph.

    python3 falsify.py https://interstellarblends.com
    python3 falsify.py https://some-competitor.example

Interstellar Blends publishes this so its own claims can be audited, and so the
same test can be run against anyone making the same kind of claim. Standard
library only. No key, no install, no rate limit.

WHAT IT IS FOR. A convincing evidence graph is cheap to fabricate: a language
model will happily emit ten thousand arrows from ingredient to pathway, each
with a plausible-looking citation. Such a graph dies under a script that asks
four dull questions:

    does the identifier resolve in form, or is it invented
    does every claim carry one at all
    do the published numbers match the data underneath them
    and is the SAME paper being counted as independent support many times over

The last is the one fabrication cannot survive. Breadth is free to invent;
independence is not. A graph that reuses one identifier across forty arrows and
calls each of them corroborated is making forty claims off one stream.

HOW TO READ THE RESULT. Every check prints PASS, FAIL or N/A with a count and a
denominator. N/A is not a pass -- it means the site publishes nothing to test,
which is itself a finding. The exit code is 0 only if no check FAILED.
"""
import json, re, sys, collections, urllib.request, urllib.error

TIMEOUT = 30
UA = {'User-Agent': 'falsify.py/1.0 (+https://interstellarblends.com/falsify/)'}

# A well-formed identifier. Anything outside these shapes is not a receipt.
# An identifier is published in more than one shape, and a checker that knows
# only one of them manufactures its own failures. MEASURED on a real corpus:
# the same PMID appears as "pmid:29804119" AND as the bare resolving URL
# "https://pubmed.ncbi.nlm.nih.gov/29804119/", where the number is a path
# segment and carries no prefix at all. A harness blind to the second form
# reports a site as unreceipted when every claim is in fact receipted. Both
# forms are first-class here, for PubMed, PMC, DOI and the trial registries.
ID_ANY = re.compile(
    r'pmid[:\s]\s*[0-9]{5,8}'          # "pmid: 123", "pmid 123" -- both ship
    r'|pubmed\.ncbi\.nlm\.nih\.gov/[0-9]{5,8}'
    r'|pmcid[:\s]\s*PMC[0-9]{5,9}'
    r'|ncbi\.nlm\.nih\.gov/(?:pmc/)?articles/PMC[0-9]{5,9}'
    r'|\bPMC[0-9]{5,9}\b'
    r'|\b10\.[0-9]{4,9}/[^\s"<>)\]]+'
    r'|\bNCT[0-9]{8}\b'
    r'|\bNBK[0-9]{4,9}\b'              # NCBI Bookshelf, a retrievable record
    r'|isbn[:\s]\s*[0-9Xx-]{10,17}', re.I)

def well_formed(tok):
    """a token ID_ANY matched is well formed by construction; this re-checks
    the few shapes that can be truncated, so C2 measures something real."""
    t = tok.strip().rstrip('.,;')
    if re.search(r'10\.[0-9]{4,9}/', t):
        return len(t.split('/', 1)[-1]) >= 3          # a DOI suffix, not a stub
    if re.search(r'PMC[0-9]{5,9}', t, re.I): return True
    if re.search(r'[0-9]{5,8}', t): return True
    if re.match(r'(?i)^isbn', t): return True
    return bool(re.match(r'(?i)^NCT[0-9]{8}$', t))


def get(url, as_json=True):
    try:
        with urllib.request.urlopen(urllib.request.Request(url, headers=UA),
                                    timeout=TIMEOUT) as r:
            raw = r.read()
    except Exception as e:                       # noqa: BLE001 -- any failure is "absent"
        return None, str(e)
    if not as_json:
        return raw.decode('utf-8', 'replace'), None
    try:
        return json.loads(raw.decode('utf-8')), None
    except ValueError as e:
        return None, 'not JSON: %s' % e


def walk(o):
    if isinstance(o, dict):
        yield o
        for v in o.values():
            for x in walk(v): yield x
    elif isinstance(o, list):
        for v in o:
            for x in walk(v): yield x


def ident_strings(node):
    """every identifier-looking string attached to one node"""
    out = []
    for o in walk(node):
        for k in ('identifier', 'sameAs', 'pmid', 'doi', 'citation', '@id', 'url'):
            v = o.get(k) if isinstance(o, dict) else None
            if isinstance(v, str): out.append(v)
            elif isinstance(v, list): out += [x for x in v if isinstance(x, str)]
    return out


class Report:
    def __init__(self): self.rows = []
    def add(self, code, name, verdict, got, of, note=''):
        self.rows.append((code, name, verdict, got, of, note))
    def show(self):
        w = max(len(r[1]) for r in self.rows)
        print('')
        print('  %-4s %-*s  %-4s  %s' % ('', w, 'CHECK', 'R', 'RESULT'))
        print('  ' + '-' * (w + 26))
        for code, name, v, got, of, note in self.rows:
            frac = '%d/%d' % (got, of) if of else '-'
            print('  %-4s %-*s  %-4s  %s  %s' % (code, w, name, v, frac, note))
        bad = [r for r in self.rows if r[2] == 'FAIL']
        na = [r for r in self.rows if r[2] == 'N/A']
        print('')
        print('  %d checks ran, %d FAILED, %d had nothing to test.'
              % (len(self.rows), len(bad), len(na)))
        if na:
            print('  Nothing to test is not a pass. It means the site does not '
                  'publish the data.')
        return 1 if bad else 0


def main(base):
    base = base.rstrip('/')
    print('falsify.py  ->  %s' % base)
    R = Report()

    graph, gerr = get(base + '/graph.jsonld')
    if graph is None:
        print('  no /graph.jsonld (%s)' % gerr)
        R.add('C1', 'publishes a machine-readable claim graph', 'FAIL', 0, 1,
              'absent: nothing here can be audited')
        return R.show()
    nodes = graph['@graph'] if isinstance(graph, dict) and '@graph' in graph else graph
    if not isinstance(nodes, list) or not nodes:
        R.add('C1', 'publishes a machine-readable claim graph', 'FAIL', 0, 1, 'empty')
        return R.show()
    R.add('C1', 'publishes a machine-readable claim graph', 'PASS', len(nodes),
          len(nodes), '%d nodes' % len(nodes))

    # ---- C2 identifier form -------------------------------------------------
    raw_ids = set()
    for n in nodes:
        for s in ident_strings(n):
            for m in ID_ANY.finditer(s): raw_ids.add(m.group(0))
    wellformed = [i for i in raw_ids if well_formed(i)]
    if raw_ids:
        R.add('C2', 'every identifier is well formed',
              'PASS' if len(wellformed) == len(raw_ids) else 'FAIL',
              len(wellformed), len(raw_ids),
              'malformed: %s' % sorted(set(raw_ids) - set(wellformed))[:3]
              if len(wellformed) != len(raw_ids) else '')
    else:
        R.add('C2', 'every identifier is well formed', 'N/A', 0, 0,
              'the graph carries no identifiers')

    # ---- C3 is every claim backed by a receipt ------------------------------
    # A node carries its own identifier, OR it is a concept whose assertion
    # lives on its edges -- a goal is not backed by a study about the phrase
    # "weight loss", it is backed by the pathways it declares. So a node with
    # no identifier of its own passes only if EVERY edge asserting it is
    # itself receipted. Every one, not most: one unreceipted driver and the
    # concept is unbacked. Both numbers are reported, so a site cannot hide a
    # thin direct count behind transitive credit.
    claim_types = ('Substance', 'MedicalCondition', 'DefinedTerm')
    claimers = [n for n in nodes
                if any(t in str(n.get('@type', '')) for t in claim_types)]
    byid = {n.get('@id'): n for n in nodes if isinstance(n, dict)}
    ASSERT_RELS = ('drivenByPathway', 'actsOnPathway', 'connectedPathway',
                   'modulatedByBlend', 'addressedByBlend')

    def receipted(n):
        return any(ID_ANY.search(s) for s in ident_strings(n))

    direct, through, unbacked = [], [], []
    for n in claimers:
        if receipted(n):
            direct.append(n); continue
        edges = []
        for rel in ASSERT_RELS:                      # edges this node declares
            v = n.get(rel)
            if isinstance(v, list): edges += [x for x in v if isinstance(x, str)]
        for m in nodes:                              # edges declared at this node
            if not isinstance(m, dict): continue
            for rel in ASSERT_RELS:
                v = m.get(rel)
                if isinstance(v, list) and n.get('@id') in v:
                    edges.append(m.get('@id'))
        srcs = [byid[e] for e in set(edges) if e in byid]
        if srcs and all(receipted(s) for s in srcs): through.append(n)
        else: unbacked.append(n)
    if claimers:
        R.add('C3', 'every claim is backed by a receipt',
              'PASS' if not unbacked else 'FAIL',
              len(direct) + len(through), len(claimers),
              '%d carry one directly, %d through fully receipted edges%s'
              % (len(direct), len(through),
                 '' if not unbacked else '; UNBACKED: %s'
                 % [str(x.get('name'))[:28] for x in unbacked[:3]]))
    else:
        R.add('C3', 'every claim is backed by a receipt', 'N/A', 0, 0,
              'no typed claim nodes found')

    # ---- C4 no dangling edge -----------------------------------------------
    ids = set(n.get('@id') for n in nodes if isinstance(n, dict))
    RELS = ('containsBotanical', 'carriedInBlend', 'actsOnPathway',
            'modulatedByBlend', 'drivenByPathway', 'addressedByBlend',
            'connectedPathway')
    edges = 0; dangle = set()
    for n in nodes:
        for rel in RELS:
            v = n.get(rel)
            if not isinstance(v, list): continue
            for t in v:
                tid = t if isinstance(t, str) else (t or {}).get('@id')
                if not tid: continue
                edges += 1
                if tid not in ids: dangle.add(tid)
    if edges:
        R.add('C4', 'every edge points at a node that exists',
              'PASS' if not dangle else 'FAIL', edges - len(dangle), edges,
              'dangling: %s' % sorted(dangle)[:2] if dangle else '')
    else:
        R.add('C4', 'every edge points at a node that exists', 'N/A', 0, 0,
              'the graph publishes no edges')

    # ---- C5 does a published count match the list it sits on ---------------
    # A count that disagrees with its own list is a contradiction unless the
    # document says why. This asks no question about the site's URL layout, so
    # it runs anywhere: find the count, find the list, compare. A difference is
    # allowed ONLY if the document publishes a definition explaining it, which
    # is cheap to honor and expensive to fake.
    eng, _ = get(base + '/engine/engine.json')
    idx = []
    names = list((eng.get('counts') or {}).keys()) if isinstance(eng, dict) else []
    for k in names or ('goals', 'pathways', 'substances', 'blends'):
        d, _ = get('%s/engine/%s.json' % (base, k))
        if not isinstance(d, dict): continue
        lst = max((v for v in d.values() if isinstance(v, list)), key=len, default=None)
        cnt = next((d[c] for c in ('count', 'total', '%s_count' % k)
                    if isinstance(d.get(c), int)), None)
        if lst is None or cnt is None: continue
        why = any('definition' in str(x).lower() for x in d)
        idx.append((k, cnt, len(lst), why))
    if idx:
        okc = [r for r in idx if r[1] == r[2] or r[3]]
        notes = ['%s: count %d over a list of %d, undeclared' % (r[0], r[1], r[2])
                 for r in idx if r not in okc]
        R.add('C5', 'a published count matches its own list',
              'PASS' if len(okc) == len(idx) else 'FAIL', len(okc), len(idx),
              '; '.join(notes))
    else:
        R.add('C5', 'a published count matches its own list', 'N/A', 0, 0,
              'no index resource publishes a count')

    # ---- C6 INDEPENDENCE: is one paper doing many jobs ---------------------
    # The expensive truth. One identifier shared across many claims is ONE
    # stream, however many arrows lean on it.
    per_id = collections.Counter()
    for n in claimers:
        for i in set(m.group(0).lower() for s in ident_strings(n)
                     for m in ID_ANY.finditer(s)):
            per_id[i] += 1
    if per_id:
        worst, worst_n = per_id.most_common(1)[0]
        reused = sum(1 for v in per_id.values() if v > 1)
        # a graph where most identifiers are load-bearing many times over is
        # breadth without streams. Report it; the reader judges the threshold.
        share = worst_n / float(len(claimers)) if claimers else 0
        R.add('C6', 'no single paper carries the graph', 
              'PASS' if share < 0.10 else 'FAIL', len(per_id), len(per_id),
              'most reused identifier backs %d nodes (%.1f%%); %d of %d ids reused'
              % (worst_n, 100 * share, reused, len(per_id)))
    else:
        R.add('C6', 'no single paper carries the graph', 'N/A', 0, 0,
              'no identifiers to weigh')

    # ---- C7 grade present and legible --------------------------------------
    graded = [n for n in nodes if 'Substance' in str(n.get('@type', ''))]
    withg = [n for n in graded if n.get('isbGrade') or n.get('evidenceLevel')]
    if graded:
        R.add('C7', 'graded claims publish their grade',
              'PASS' if len(withg) == len(graded) else 'FAIL',
              len(withg), len(graded),
              'a graded-evidence claim whose export omits the grade '
              'cannot be checked' if len(withg) != len(graded) else '')
    else:
        R.add('C7', 'graded claims publish their grade', 'N/A', 0, 0, '')

    # ---- C8 does the graph's own vocabulary resolve ------------------------
    # A graph that invents relations has to define them somewhere. This does NOT
    # guess which terms are standard -- enumerating schema.org offline is not
    # possible and a guessed allowlist produces false accusations. It asks only
    # what can be settled from the document: is there a @context, does it
    # declare a namespace of the site's own, is every term it declares actually
    # used, and does the namespace document answer when fetched.
    ctx = graph.get('@context') if isinstance(graph, dict) else None
    parts = ctx if isinstance(ctx, list) else [ctx]
    declared, prefixes = {}, {}
    for part in parts:
        if isinstance(part, dict):
            for k, v in part.items():
                if k.startswith('@'): continue
                if isinstance(v, str) and v.startswith('http'): prefixes[k] = v
                else: declared[k] = v
        elif isinstance(part, str) and part.startswith('http'):
            prefixes['(imported)'] = part
    used = set()
    for n in nodes:
        if isinstance(n, dict): used |= {k for k in n if not k.startswith('@')}
    if not parts or not any(isinstance(p, (dict, str)) for p in parts):
        R.add('C8', "the graph's own vocabulary resolves", 'FAIL', 0, 1,
              'no @context: the relations are undefined')
    else:
        dead = sorted(k for k in declared if k not in used)
        ns = [v for k, v in prefixes.items() if k != '(imported)']
        reach = 0
        for u in ns[:2]:
            body, err = get(u.split('#')[0], as_json=False)
            if body: reach += 1
        notes = []
        if dead: notes.append('declared but never used: %s' % dead[:4])
        if ns and not reach: notes.append('namespace did not answer: %s' % ns[:1])
        ok = (not dead) and (not ns or reach)
        R.add('C8', "the graph's own vocabulary resolves",
              'PASS' if ok else 'FAIL', len(declared) - len(dead), len(declared),
              '; '.join(notes) or '%d terms declared, %d namespace(s) reachable'
              % (len(declared), reach))

    # ---- C9 the causal map: is every arrow sourced -----------------------
    # The graph says WHAT connects to what. An atlas says which DIRECTION and
    # on whose authority. A site can publish a tidy graph and still never state
    # why any arrow points the way it does, so this asks for both ends, a
    # direction, and a citation on every edge -- and reports how many of those
    # citations carry an identifier you can actually retrieve, which is the
    # number a reader should judge the map by.
    atlas, aerr = get(base + '/atlas.jsonld')
    obs = []
    if isinstance(atlas, (dict, list)):
        for o in walk(atlas):
            if str(o.get('@type', '')) == 'Observation' or (
                    o.get('measuredProperty') and (o.get('object') or o.get('subjectOf'))):
                obs.append(o)
    if not obs:
        R.add('C9', 'every causal arrow is sourced', 'N/A', 0, 0,
              'no /atlas.jsonld with typed edges (%s)' % (aerr or 'none found'))
    else:
        incomplete = []
        with_id = 0
        for o in obs:
            ends = (o.get('subjectOf') or o.get('subject')) and o.get('object')
            direction = o.get('measuredProperty')
            cite = str(o.get('citation') or '').strip()
            if not (ends and direction and cite):
                incomplete.append(str(o.get('name') or o.get('@id'))[:40])
            blob = cite + ' ' + str(o.get('text') or '')
            if ID_ANY.search(blob) or re.search(r'NBK[0-9]+', blob, re.I):
                with_id += 1
        pct = 100.0 * with_id / len(obs)
        R.add('C9', 'every causal arrow is sourced',
              'PASS' if not incomplete else 'FAIL',
              len(obs) - len(incomplete), len(obs),
              '%d of %d citations carry a retrievable identifier (%.1f%%); '
              'the rest name a reference work in prose%s'
              % (with_id, len(obs), pct,
                 '' if not incomplete else '; INCOMPLETE: %s' % incomplete[:3]))

    code = R.show()
    print('')
    print('  Run this against anyone publishing a pathway or ingredient claim')
    print('  graph. If a site has no /graph.jsonld, C1 fails and there is')
    print('  nothing further to check -- which is the answer.')
    print('')
    return code


if __name__ == '__main__':
    if len(sys.argv) != 2 or sys.argv[1] in ('-h', '--help'):
        print(__doc__)
        sys.exit(2)
    sys.exit(main(sys.argv[1]))
