"""Rebuild and check the pinned ArchiSurance example. Without flags, read only the CSVs."""
import argparse
import collections
import csv
import hashlib
import io
import json
import os
from pathlib import Path
import re
from urllib.request import Request, urlopen

REVISION = "bebb29d8497dd48d0f3133e792019bac540fa6c8"
BASE = f"https://raw.githubusercontent.com/lju-lazarevic/misc/{REVISION}/archi"
HASHES = {
    "elements": "53eb34c6b6f116530b3db0f1824df03d147edc91aca54ca27a5ffc2dd20a82b1",
    "relations": "bfb9ac8d0dd1209a89636083df40dfcad233550e71944f1d9e0d67677315c22d",
    "properties": "c03a7a02b3eff303d7291257874ca1e1508e980914a41c80734f05e539de6a57",
}
NODE_QUERY = """
MATCH (n:F382ArchiElement)
RETURN n.id AS id, n.name AS name, n.class AS class, n.documentation AS documentation
ORDER BY id
"""
EDGE_QUERY = """
MATCH (s:F382ArchiElement)-[r]->(t:F382ArchiElement)
WHERE r.experiment = '382.archisurance'
RETURN s.id AS source, t.id AS target, r.id AS id, type(r) AS class,
       r.name AS name, r.documentation AS documentation,
       r.Access_Type AS Access_Type, r.Directed AS Directed
ORDER BY id
"""
EXPECTED_APPS = ["Calculate Risk", "Claim Data Management", "Policy Creation"]
APP_TYPES = {"ApplicationComponent", "ApplicationFunction", "ApplicationService"}


def read_csv(stem, directory):
    if directory is None:
        req = Request(f"{BASE}/{stem}.csv", headers={"User-Agent": "Foray382-replay"})
        with urlopen(req, timeout=30) as response:
            data = response.read()
    else:
        data = (Path(directory) / (stem + ".csv")).read_bytes()
    if hashlib.sha256(data).hexdigest() != HASHES[stem]:
        raise ValueError(f"{stem}.csv differs from the pinned source; stopping.")
    return list(csv.DictReader(io.StringIO(data.decode("utf-8-sig"))))


def source_tables(directory=None):
    nodes = [dict(id=r["ID"], name=r["Name"], **{"class": r["Type"]},
                  documentation=r["Documentation"]) for r in read_csv("elements", directory)]
    edges = [dict(id=r["ID"], name=r["Name"], source=r["Source"], target=r["Target"],
                  **{"class": r["Type"]}, documentation=r["Documentation"],
                  Access_Type=None, Directed=None) for r in read_csv("relations", directory)]
    by_id = {e["id"]: e for e in edges}
    for record in read_csv("properties", directory):
        by_id[record["ID"]][record["Key"]] = record["Value"]
    return sorted(nodes, key=lambda n: n["id"]), sorted(edges, key=lambda e: e["id"])


def summary(nodes, edges):
    incident = {e[k] for e in edges for k in ("source", "target")}
    incoming = {e["target"] for e in edges}
    isolates = sorted(n["name"] for n in nodes if n["id"] not in incident)
    apps = sorted(n["name"] for n in nodes if n["class"] in APP_TYPES and n["id"] not in incoming)
    if (len(nodes), len(edges), len(incident), len(isolates), apps) != (121, 176, 103, 18, EXPECTED_APPS):
        raise ValueError("Graph differs from the independent element/relationship check.")
    return {"elements": len(nodes), "relationships": len(edges), "nodes_in_edge_view": len(incident),
            "isolated_element_names": isolates, "applications_without_incoming_links": apps}


def connection():
    from neo4j import GraphDatabase
    uri = os.environ["NEO4J_URI"]
    return GraphDatabase.driver(uri, auth=(os.environ["NEO4J_USERNAME"], os.environ["NEO4J_PASSWORD"]))


def database_tables(tx):
    return list(tx.run(NODE_QUERY).data()), list(tx.run(EDGE_QUERY).data())


def check_database(tx, nodes, edges):
    db_nodes, db_edges = database_tables(tx)
    if (db_nodes, db_edges) != (nodes, edges):
        raise ValueError("Stored IDs, classes, endpoints or properties differ from the pinned source.")
    return summary(db_nodes, db_edges)


def grouped(rows):
    groups = collections.defaultdict(list)
    for row in rows:
        name = row["class"]
        if not re.fullmatch(r"[A-Za-z][A-Za-z0-9_]*", name):
            raise ValueError("Unexpected source class.")
        groups[name].append(row)
    return groups.items()


def load(tx, nodes, edges):
    # Classic Cypher syntax works on Neo4j 5 and Aura. Only the scoped label
    # and experiment-marked relationships are written. The typed identifiers
    # come from the verified source and pass an identifier check above.
    for cls, rows in grouped(nodes):
        tx.run("UNWIND $rows AS v MERGE (n:F382ArchiElement {id:v.id}) "
               f"SET n:{cls}, n.name=v.name, n.class=v.class, n.documentation=v.documentation",
               rows=rows).consume()
    for cls, rows in grouped(edges):
        tx.run("UNWIND $rows AS v MATCH (s:F382ArchiElement {id:v.source}), "
               "(t:F382ArchiElement {id:v.target}) "
               f"MERGE (s)-[r:{cls} {{experiment:'382.archisurance',id:v.id}}]->(t) "
               "SET r.name=v.name, r.documentation=v.documentation, "
               "r.Access_Type=v.Access_Type, r.Directed=v.Directed", rows=rows).consume()
    return check_database(tx, nodes, edges)


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    mode = parser.add_mutually_exclusive_group()
    mode.add_argument("--load", action="store_true", help="Merge and check this experiment in Neo4j.")
    mode.add_argument("--check", action="store_true", help="Verify the stored graph against all source rows.")
    parser.add_argument("--source-dir", type=Path, help="Directory of the three original CSVs (checksummed).")
    args = parser.parse_args()
    nodes, edges = source_tables(args.source_dir)
    result = summary(nodes, edges)
    if args.load or args.check:
        from neo4j import READ_ACCESS, WRITE_ACCESS
        mode = WRITE_ACCESS if args.load else READ_ACCESS
        with connection() as driver, driver.session(database=os.getenv("NEO4J_DATABASE", "neo4j"),
                                                    default_access_mode=mode) as sess:
            if args.load:
                sess.run("CREATE CONSTRAINT f382_archi_element_id IF NOT EXISTS "
                         "FOR (n:F382ArchiElement) REQUIRE n.id IS UNIQUE").consume()
                result = sess.execute_write(load, nodes, edges)
            else:
                result = sess.execute_read(check_database, nodes, edges)
    print(json.dumps(result, indent=2, ensure_ascii=False))


if __name__ == "__main__":
    main()
