#!/usr/bin/env python3
import hashlib
import json
import re
import sys
import unicodedata
from datetime import datetime, timezone
from pathlib import Path

PROJECT = Path(__file__).resolve().parents[1]
PROJECTS = PROJECT.parent
CANON = PROJECT / "story/story-canon.txt"
BRIEF = PROJECT / "script/story-brief.json"
RECEIPT = PROJECT / "story/originality-report.json"
TOKEN_RE = re.compile(r"[^\W_]+", re.UNICODE)


def sha(path):
    return hashlib.sha256(path.read_bytes()).hexdigest()


def tokens(text):
    normalized = unicodedata.normalize("NFC", text).casefold()
    return TOKEN_RE.findall(normalized)


def shingles(items, width=10):
    return {" ".join(items[index:index + width]) for index in range(max(0, len(items) - width + 1))}


def main():
    if not CANON.is_file() or not BRIEF.is_file():
        raise RuntimeError("canon/brief missing")
    current_tokens = tokens(CANON.read_text(encoding="utf-8"))
    current = shingles(current_tokens)
    comparisons = []
    worst = 0.0
    for directory in sorted(PROJECTS.iterdir()):
        if directory == PROJECT or not directory.is_dir():
            continue
        match = re.match(r"^(\d+)", directory.name)
        if not match or int(match.group(1)) < 2:
            continue
        candidates = [directory / "story/story-canon.txt", directory / "story/spoken-narration.txt"]
        other = next((path for path in candidates if path.is_file()), None)
        if other is None:
            continue
        other_set = shingles(tokens(other.read_text(encoding="utf-8")))
        overlap = len(current & other_set)
        ratio = overlap / max(1, len(current))
        worst = max(worst, ratio)
        comparisons.append({"project": directory.name, "path": str(other), "overlap_10grams": overlap, "current_overlap_ratio": ratio})
    brief = json.loads(BRIEF.read_text(encoding="utf-8"))
    required_axes = ["setting", "premise", "distinctive_engine", "motifs", "ending"]
    authority_complete = all(brief.get(key) for key in required_axes)
    verified = authority_complete and worst < 0.002
    receipt = {
        "version": 1, "verified": verified, "status": "passed" if verified else "failed",
        "source_canon_sha256": sha(CANON), "method": "normalized Unicode 10-word shingle comparison",
        "threshold": 0.002, "worst_current_overlap_ratio": worst,
        "authority_axes_complete": authority_complete, "comparisons": comparisons,
        "checked_at": datetime.now(timezone.utc).isoformat(),
    }
    RECEIPT.write_text(json.dumps(receipt, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
    print(json.dumps({"verified": verified, "comparisons": len(comparisons), "worst_ratio": worst}, ensure_ascii=False))
    return 0 if verified else 1


if __name__ == "__main__":
    try:
        sys.exit(main())
    except Exception as exc:
        print(f"Originality gate blocked: {exc}", file=sys.stderr)
        sys.exit(1)
