"""Reproduce Foray 324's FDA investment comparison from the published Library files."""
import argparse
import hashlib
import re
from collections import Counter
from pathlib import Path
from urllib.request import urlopen


LIBRARY = (
    "https://experimentsin.ai/library/sources/us-portfolio/files/"
    "Example_0_a_thousand_US_projects/cypher_code/"
)
FILES = {
    "Health_sub_graph.cypher": "98d042889dff84864eacf2b17d319db5484b33bed76a354e6eb34196da2f9afc",
    "Complete_graph.cypher": "c67d6b0b9fa74f8c81477d0ccaf266d0e77ba12a5af9ecca2ea8896924399095",
}
INVESTMENT = "009-000005317"
NODE = re.compile(r'^CREATE \(:(\w+):.*?`UNIQUE IMPORT ID`:(\d+)\}\);$')
EDGE = re.compile(r'^MATCH .*?`UNIQUE IMPORT ID`:(\d+)\}.*?`UNIQUE IMPORT ID`:(\d+)\}.*?\[r:(\w+)\]->\(n2\);$')
FIELDS = re.compile(r'(?:[ {,])(Id|ID|Identifier|Metric_results):"([^"]*)"')


def load(name, directory):
    if directory:
        data = (directory / name).read_bytes()
    else:
        with urlopen(LIBRARY + name, timeout=45) as response:
            data = response.read()
    if hashlib.sha256(data).hexdigest() != FILES[name]:
        raise ValueError(f"The contents of {name} differ from the snapshot used in the essai.")
    return data.decode("utf-8").splitlines()


def inspect(lines):
    nodes = {}
    for line in lines:
        match = NODE.match(line)
        if match:
            label, node_id = match.groups()
            nodes[node_id] = label, dict(FIELDS.findall(line))
    targets = [nid for nid, (label, fields) in nodes.items()
               if label == "Investment" and fields.get("Identifier") == INVESTMENT]
    if len(targets) != 1:
        raise ValueError(f"Expected exactly one investment with identifier {INVESTMENT}.")
    projects = set()
    measures = {}
    for line in lines:
        match = EDGE.match(line)
        if not match or match[1] != targets[0]:
            continue
        label, fields = nodes[match[2]]
        if match[3] == "pays_for" and label == "Project":
            projects.add(fields["Id"])
        if match[3] == "judged_by" and label == "Metric":
            measures[fields["ID"]] = fields.get("Metric_results", "unreported")
    # Pair each project with each measure through their common investment.
    joined = [(project, measure) for project in projects for measure in measures]
    return projects, measures, joined


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--directory", type=Path, help="Folder containing the two Library files, for offline use.")
    args = parser.parse_args()
    results = []
    for name in FILES:
        projects, measures, joined = inspect(load(name, args.directory))
        print(f"{name}: {len(projects)} distinct projects, {len(measures)} distinct measures, "
              f"{len(joined)} project/measure pairs")
        print("  Measure ID and recorded outcome:", ", ".join(
            f"{key} = {measures[key]}" for key in sorted(measures)))
        print("  Pairs per measure:", dict(sorted(Counter(measure for _, measure in joined).items())))
        results.append((projects, measures, len(joined)))
    health, complete = results
    if not (len(health[0]) == 9 and health[0] == complete[0] and
            health[1] == {"3479": "unreported", "3480": "unreported", "3481": "Not Met",
                          "3482": "unreported", "3483": "unreported"} and
            complete[1] == {"3481": "Not Met"} and (health[2], complete[2]) == (45, 9)):
        raise ValueError("The source relationships do not reproduce the published result.")
    print("The nine project IDs match across both exports.")


if __name__ == "__main__":
    main()
