#!/usr/bin/env python3
"""
Tests for ocr.py and redact.py against a generated PDF. Needs Tesseract for the flat pages, and
pdftotext (poppler-utils) for an independent check of the output.

Usage: scripts/workshop-doc/.venv/bin/python scripts/workshop-doc/test_redact.py
"""

import json
import os
import shutil
import subprocess
import sys
import tempfile

import pymupdf

HERE = os.path.dirname(os.path.abspath(__file__))
failures = 0


def check(ok, what):
    global failures
    print(("  ok    " if ok else "  FAIL  ") + what)
    if not ok:
        failures += 1


def flat_page(doc, lines, invisible_layer=False):
    """A page that is only an image of text, like a scan."""
    src = pymupdf.open()
    p = src.new_page()
    for i, line in enumerate(lines):
        p.insert_text((72, 120 + i * 30), line, fontsize=16)
    pix = p.get_pixmap(dpi=150)
    page = doc.new_page()
    page.insert_image(page.rect, pixmap=pix)
    if invisible_layer:
        for i, line in enumerate(lines):
            page.insert_text((72, 120 + i * 30), line, fontsize=16, render_mode=3)


def build(path):
    doc = pymupdf.open()

    # 1: digital, tight leading, with everything that has to be stripped.
    p = doc.new_page()
    p.insert_text((72, 100), "Customer: John Smith", fontsize=12, fontname="helv")
    p.insert_text((72, 112), "SSN 555-12-3456 on file", fontsize=12, fontname="tiro")
    p.insert_text((72, 124), "Address line stays intact", fontsize=12, fontname="cour")
    p.insert_text((72, 200), "Wrapping value: Alexandra", fontsize=11)
    p.insert_text((72, 213), "Montgomery-Hughes and more", fontsize=11)
    p.add_text_annot((300, 300), "note about John Smith")
    p.insert_link({"kind": pymupdf.LINK_URI, "from": pymupdf.Rect(72, 300, 200, 320), "uri": "mailto:john.smith@example.com"})
    w = pymupdf.Widget()
    w.field_name = "customer"
    w.field_type = pymupdf.PDF_WIDGET_TYPE_TEXT
    w.field_value = "John Smith"
    w.rect = pymupdf.Rect(72, 400, 300, 420)
    p.add_widget(w)

    # 2: flat (OCR).  3: digital on a rotated page.  4: flat with an invisible OCR layer.
    flat_page(doc, ["Patient ID 98765-4321", "Keep this line"])
    p = doc.new_page()
    p.insert_text((72, 100), "Rotated Rebecca Thornbury here", fontsize=12)
    p.set_rotation(90)
    flat_page(doc, ["Account 44556677 closed"], invisible_layer=True)

    doc.set_metadata({"author": "John Smith", "title": "Smith file"})
    doc.set_xml_metadata("<x:xmpmeta xmlns:x='adobe:ns:meta/'>John Smith</x:xmpmeta>")
    doc.embfile_add("secret.txt", b"John Smith 555-12-3456")
    doc.set_toc([[1, "John Smith", 1]])
    doc.save(path)


def run(script, *args):
    r = subprocess.run([sys.executable, os.path.join(HERE, script), *args], capture_output=True, text=True)
    try:
        return json.loads(r.stdout)
    except ValueError:
        print(r.stdout, r.stderr)
        raise


def boxes(pages, n, *texts):
    return [w["bbox"] for w in pages[n - 1]["words"] if w["text"] in texts]


def union(rects, pad=1):
    return [min(r[0] for r in rects) - pad, min(r[1] for r in rects) - pad,
            max(r[2] for r in rects) + pad, max(r[3] for r in rects) + pad]


def main():
    tmp = tempfile.mkdtemp()
    try:
        src = os.path.join(tmp, "in.pdf")
        build(src)

        print("ocr.py")
        res = run("ocr.py", src)
        check(res["status"] == "ok", "runs")
        pages = res["pages"]
        check([p["kind"] for p in pages] == ["digital", "flat", "digital", "flat"], "detects digital and flat pages")
        check(len(boxes(pages, 1, "John", "Smith")) >= 2, "digital words")
        check(any("98765" in w["text"] for w in pages[1]["words"]), "flat page is OCR'd")
        check(pages[2]["width"] == 842 and pages[2]["height"] == 595, "rotated page size is as displayed")
        rb = boxes(pages, 3, "Rebecca")
        check(bool(rb) and rb[0][2] - rb[0][0] < 15 and rb[0][3] - rb[0][1] > 30, "rotated words are in display coordinates")
        check(any("44556677" in w["text"] for w in pages[3]["words"]), "invisible text layer is used as-is")
        check(run("ocr.py", os.path.join(HERE, "test_redact.py"))["status"] == "error", "rejects a non-PDF")

        print("redact.py")
        spec = {"redactions": [
            {"page": 1, "mode": "replace", "rects": boxes(pages, 1, "John", "Smith")[:2], "replacement": "Jane Doe"},
            {"page": 1, "mode": "replace", "rects": boxes(pages, 1, "555-12-3456"), "replacement": "000-00-0000"},
            {"page": 1, "mode": "replace", "rects": boxes(pages, 1, "Alexandra", "Montgomery-Hughes"), "replacement": "Bea Ray-Lopez"},
            {"page": 2, "mode": "remove", "rects": [union([w["bbox"] for w in pages[1]["words"] if "98765" in w["text"]])]},
            {"page": 3, "mode": "replace", "rects": boxes(pages, 3, "Rebecca", "Thornbury"), "replacement": "Ann Lee"},
            {"page": 4, "mode": "remove", "rects": [union([w["bbox"] for w in pages[3]["words"] if "44556677" in w["text"]])]},
        ]}
        spec_path = os.path.join(tmp, "r.json")
        out = os.path.join(tmp, "out.pdf")
        with open(spec_path, "w") as f:
            json.dump(spec, f)
        res = run("redact.py", src, spec_path, out)
        check(res["status"] == "ok", f"runs ({res.get('message', '')})")

        text = subprocess.run(["pdftotext", "-layout", out, "-"], capture_output=True, text=True).stdout
        for gone in ["John", "Smith", "555-12-3456", "Alexandra", "Montgomery", "Rebecca", "Thornbury", "44556677"]:
            check(gone not in text, f"pdftotext: no {gone}")
        for kept in ["Jane Doe", "000-00-0000", "Customer:", "on file", "Address line stays intact", "Ann Lee", "and more"]:
            check(kept in text, f"pdftotext: has {kept!r}")

        doc = pymupdf.open(out)
        raw = b"".join(doc.xref_object(x, compressed=False).encode() + ((doc.xref_stream(x) or b"") if doc.xref_is_stream(x) else b"")
                       for x in range(1, doc.xref_length()))
        for gone in [b"John", b"Smith", b"555-12-3456", b"44556677", b"secret.txt", b"mailto"]:
            check(gone not in raw, f"raw objects: no {gone.decode()}")
        check(not any(v for v in doc.metadata.values() if v and v != "PDF 1.7" and not v.startswith("PDF")), "metadata stripped")
        check(not doc.get_xml_metadata(), "XMP stripped")
        check(doc.embfile_count() == 0, "attachments stripped")
        check(not doc.get_toc(), "bookmarks stripped")
        check(all(not list(p.annots()) and not list(p.widgets()) and not p.get_links() for p in doc), "annotations, fields, links stripped")

        # Remove mode: the area is black in the image itself, not just covered.
        p2 = doc[1]
        r = pymupdf.Rect(spec["redactions"][3]["rects"][0])
        imgs = p2.get_images(full=True)
        pix = pymupdf.Pixmap(doc, imgs[0][0]) if imgs else None
        if pix:
            sx, sy = pix.width / p2.rect.width, pix.height / p2.rect.height
            cx, cy = int((r.x0 + r.x1) / 2 * sx), int((r.y0 + r.y1) / 2 * sy)
            check(sum(pix.pixel(cx, cy)[:3]) < 60 or pix.pixel(cx, cy)[:3] == (255, 255, 255), "image pixels inside the area are gone")
        ocr = p2.get_textpage_ocr(dpi=300, full=True)
        check("Keep this line" in p2.get_text(textpage=ocr), "OCR: rest of the flat page intact")
        doc.close()

        print("redact.py leftover check")
        twice = os.path.join(tmp, "twice.pdf")
        d = pymupdf.open()
        p = d.new_page()
        p.insert_text((72, 100), "Name Zebulon Quartermaine", fontsize=12)
        p.insert_text((72, 200), "Signed Zebulon Quartermaine", fontsize=12)
        d.save(twice)
        tw = run("ocr.py", twice)["pages"]
        first = [w["bbox"] for w in tw[0]["words"][:3] if w["text"] in ("Zebulon", "Quartermaine")]
        with open(spec_path, "w") as f:
            json.dump({"redactions": [{"page": 1, "mode": "replace", "rects": first, "replacement": "Ann Lee"}]}, f)
        res = run("redact.py", twice, spec_path, out + "2")
        check(res["status"] == "error" and res.get("fields") == [0], "fails when the value appears elsewhere")
        check("Zebulon" not in res.get("message", ""), "error doesn't echo the value")
        hits = res.get("leftovers", [])
        check(len(hits) == 2 and all(h["field"] == 0 and h["page"] == 1 for h in hits), "reports each leftover word")
        check(all(190 < h["bbox"][3] < 205 for h in hits), "leftover boxes are where the other copy is")
        check(not os.path.exists(out + "2"), "deletes the output on failure")

        # An area over a dotted leader holds no data, so it mustn't match every other leader.
        leaders = os.path.join(tmp, "leaders.pdf")
        d = pymupdf.open()
        p = d.new_page()
        p.insert_text((72, 100), "Relay ............ ON", fontsize=12)
        p.insert_text((72, 120), "Voltage ............ 11.62V", fontsize=12)
        d.save(leaders)
        lw = run("ocr.py", leaders)["pages"]
        dots = [w["bbox"] for w in lw[0]["words"] if w["text"].startswith(".")][:1]
        with open(spec_path, "w") as f:
            json.dump({"redactions": [{"page": 1, "mode": "remove", "rects": [union(dots)]}]}, f)
        res = run("redact.py", leaders, spec_path, out + "3")
        check(res["status"] == "ok", f"punctuation-only value isn't a leftover ({res.get('message', '')})")
    finally:
        shutil.rmtree(tmp)

    print("passed" if not failures else f"{failures} failed")
    sys.exit(1 if failures else 0)


if __name__ == "__main__":
    main()
