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

PROJECT = Path(__file__).resolve().parents[1]
CURRENT = PROJECT / "story/spoken-narration.txt"
REPORT = PROJECT / "story/originality-report.json"
BASE = PROJECT.parent
REFERENCES = [
    BASE / "002-Co-Ke-Toan-Bi-Ep-Nhan-Toi-Bien-Thu-Quy-Cuu-Tro-Den-Khi-Muoi-Hai-Chu-Ky-Cung-Phan-Chu/story/spoken-narration.txt",
    BASE / "003-Co-Chu-Tro-Bi-Ep-Ban-Nha-Bay-Chia-Khoa-Bong-Tro-Ve/story/spoken-narration.txt",
    BASE / "004-Co-Ky-Su-Bi-Vu-Toi-Sap-Cau-Muoi-Ba-Vet-Nut-Doi-Huong/story/spoken-narration.txt",
    BASE / "005-Co-Dau-Bep-Bi-Vu-Bo-Doc-Chin-Chiec-Dia-Cung-Len-Tieng/story/spoken-narration.txt",
    BASE / "006-Co-Nhac-Truong-Bi-Vu-Pha-Buoi-Dien-Bay-Nhip-Den-Dong-Loat-Tat/story/spoken-narration.txt",
    BASE / "007-Co-Duoc-Si-Bi-Vu-Doi-Thuoc-Tam-Lo-Thuy-Tinh-Cung-Len-Tieng/story/spoken-narration.txt",
    BASE / "008-Co-Giao-Bi-Vu-Lo-De-Thi-Chin-To-Giay-Than-Dong-Loat-Len-Tieng/story/spoken-narration.txt",
]
TOKEN_RE = re.compile(r"[\wÀ-ỹĐđ]+", re.UNICODE)


def tokens(text):
    return [token.lower() for token in TOKEN_RE.findall(text)]


def ngrams(items, size):
    return {tuple(items[i:i+size]) for i in range(max(0, len(items)-size+1))}


def main():
    if not CURRENT.exists():
        print(json.dumps({"verified": False, "issues": ["missing current narration"]}))
        return 1
    text = CURRENT.read_text()
    current = tokens(text)
    issues, comparisons = [], []
    paragraphs = [re.sub(r"\s+", " ", p.strip().lower()) for p in text.split("\n\n") if p.strip()]
    duplicate_paragraphs = [p for p, count in __import__('collections').Counter(paragraphs).items() if count > 1 and len(p) > 160]
    if duplicate_paragraphs:
        issues.append(f"duplicate long paragraphs: {len(duplicate_paragraphs)}")
    current_12 = ngrams(current, 12)
    for path in REFERENCES:
        if not path.exists():
            issues.append(f"missing originality reference: {path}")
            continue
        ref = tokens(path.read_text())
        overlap = current_12 & ngrams(ref, 12)
        ratio = len(overlap) / max(1, len(current_12))
        comparisons.append({"path": str(path), "shared_12grams": len(overlap), "ratio": ratio})
        if len(overlap) > 15 or ratio > 0.002:
            issues.append(f"excessive 12-gram overlap with {path.parent.parent.name}: {len(overlap)}")
    report = {"version": 1, "verified": not issues, "source_sha256": hashlib.sha256(text.encode()).hexdigest(), "comparisons": comparisons, "duplicate_long_paragraphs": len(duplicate_paragraphs), "issues": issues, "checked_at": datetime.now(timezone.utc).isoformat()}
    REPORT.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n")
    print(json.dumps({"verified": report["verified"], "references": len(comparisons), "issues": issues}, ensure_ascii=False))
    return 0 if report["verified"] else 1

if __name__ == "__main__":
    sys.exit(main())
