#!/usr/bin/env python3
"""Verify the corrections at TEXT level (no PDF rebuild, no rendering artifacts).
For each book: take the master's extracted text, apply the reversions via
word-level replacement (re-accented GOLD), and diff against the scan (gold).
Report diffs BEFORE vs AFTER correction — the true effect of the corrections.
"""
import compare as c, fitz, re, unicodedata, json, collections, os

BASE = os.path.dirname(os.path.abspath(__file__))
RANGES = json.load(open(os.path.join(BASE, "master_ranges.json")))
amap = json.load(open(os.path.join(BASE, "accent_map.json")))
M = fitz.open(os.path.join(BASE, "master.pdf"))

def fold(s):
    s = unicodedata.normalize('NFKD', s)
    return ''.join(ch for ch in s if not unicodedata.combining(ch)).lower()
def reaccent(s):
    return re.sub(r"[A-Za-z']+", lambda m: amap.get(fold(m.group(0)), m.group(0)), s)

def parse_revs():
    revs = collections.defaultdict(list); cur = None
    for line in open(os.path.join(BASE, "reversiones_master.md")):
        h = re.match(r'^## (\S+)', line)
        if h: cur = h.group(1)
        d = re.match(r'^- en vedabase «(.*?)»  ->  restaurar «(.*?)»', line)
        if d and cur: revs[cur].append((d.group(1), d.group(2)))
    return revs

def main():
    revs = parse_revs()
    tot_before = tot_after = 0
    rows = []
    for k in sorted(RANGES):
        if k in ("tlc", "ejop") or k == "bg": continue
        b = c.BOOKS[k]
        s, e = RANGES[k]["vedabase_start"], RANGES[k]["vedabase_end"]
        veda = ''.join(M[i].get_text() for i in range(s, min(e, len(M))))
        # gold scan text
        try:
            gold = c.extract_chapter_text(
                c.resolve_orig(b['original'] if isinstance(b['original'], str) else b['original'][0]),
                b.get('orig_start_page', 0), b.get('orig_end_page', 99999), strip_headers=True)
        except Exception:
            continue
        # apply corrections to veda by word-level replace
        words = veda.split()
        fw = [fold(w) for w in words]
        applied = 0
        for vd, gd in revs.get(k, []):
            target = fold(vd).split()
            if not target: continue
            for j in range(len(fw) - len(target) + 1):
                if fw[j:j+len(target)] == target:
                    rep = reaccent(gd).split()
                    words[j:j+len(target)] = rep
                    fw[j:j+len(target)] = [fold(x) for x in rep]
                    applied += 1; break
        veda_corr = ' '.join(words)
        nb = len(c.compare_texts(veda, gold, "full", 1, "prose"))
        na = len(c.compare_texts(veda_corr, gold, "full", 1, "prose"))
        tot_before += nb; tot_after += na
        rows.append((nb - na, nb, na, applied, k))
    rows.sort(reverse=True)
    print(f"DIFFS a nivel de TEXTO (excl. tlc/ejop/bg):")
    print(f"  antes:   {tot_before}")
    print(f"  después: {tot_after}")
    print(f"  resueltos: {tot_before - tot_after} ({round((tot_before-tot_after)/tot_before*100)}%)\n")
    print(f"{'libro':10} {'antes':>6} {'desp':>6} {'aplic':>6}")
    for d, nb, na, ap, k in rows[:20]:
        print(f"  {k:9} {nb:6} {na:6} {ap:6}")

if __name__ == "__main__":
    main()
