# -*- coding: utf-8 -*-
import json, re, importlib.util, sys

def load(modname, path):
    spec = importlib.util.spec_from_file_location(modname, path)
    m = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(m)
    return m

d1 = load('data1', 'data1.py')
d2 = load('data2', 'data2.py')
d3 = load('data3', 'data3.py')
d4 = load('data4', 'data4.py')

ALL = {}
for d in (d1, d2, d3, d4):
    ALL.update(d.NEW)

SRC = 'mdm-05.json'
DST = 'mdm-05.done.json'

data = json.load(open(SRC))

missing = [a['article_id'] for a in data if a['article_id'] not in ALL]
if missing:
    print("MISSING ARTICLE IDS:", missing)
    sys.exit(1)

extra = set(ALL.keys()) - {a['article_id'] for a in data}
if extra:
    print("EXTRA ARTICLE IDS NOT IN SOURCE:", extra)
    sys.exit(1)

out = []
for a in data:
    new_a = {
        "article_id": a["article_id"],
        "article_title": a["article_title"],
        "sections": ALL[a["article_id"]],
    }
    out.append(new_a)

json.dump(out, open(DST, 'w'), indent=2, ensure_ascii=False)
print(f"Wrote {len(out)} articles to {DST}")

# ---- verification ----
ALLOWED_TITLES = {"Clinical paths", "Diagnosis", "Management", "Prognosis and outcome"}

def nums(s):
    return sorted(re.findall(r'\d+(?:\.\d+)?', s))

def marks(s):
    return sorted(re.findall(r'\[\[(\d+)\|', s))

by_id_before = {a['article_id']: a for a in data}

fail_count = 0
prognosis_count = 0
for a in out:
    aid = a['article_id']
    before = ' '.join(s['content'] for s in by_id_before[aid]['sections'])
    after = ' '.join(s['content'] for s in a['sections'])
    titles = [s['title'] for s in a['sections']]

    problems = []
    if nums(before) != nums(after):
        problems.append(f"NUM MISMATCH before={nums(before)} after={nums(after)}")
    if marks(before) != marks(after):
        problems.append(f"MARK MISMATCH before={marks(before)} after={marks(after)}")
    ratio = len(after) / len(before)
    if not (0.8 <= ratio <= 1.3):
        problems.append(f"RATIO {ratio:.3f}")
    if len(titles) not in (3, 4):
        problems.append(f"SECTION COUNT {len(titles)}")
    else:
        if titles[0] != "Clinical paths" or titles[1] != "Diagnosis" or titles[2] != "Management":
            problems.append(f"TITLE ORDER {titles}")
        if len(titles) == 4:
            if titles[3] != "Prognosis and outcome":
                problems.append(f"4TH TITLE WRONG {titles}")
            else:
                prognosis_count += 1
        if any(t not in ALLOWED_TITLES for t in titles):
            problems.append(f"UNKNOWN TITLE {titles}")
        if len(set(titles)) != len(titles):
            problems.append(f"DUP TITLE {titles}")

    if problems:
        fail_count += 1
        print(f"[FAIL] {aid} {a['article_title']}: " + " | ".join(problems))

print()
print(f"Total articles: {len(out)}")
print(f"Failures: {fail_count}")
print(f"With Prognosis and outcome section: {prognosis_count}")
