#!/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]
CANDIDATE = PROJECT / "story" / "spoken-narration.txt"
REFERENCE = Path('/data/video-pipeline/Chanel-HuyenAnAudio/project/002-Co-Ke-Toan-Bi-Ep-Nhan-Toi-Bien-Thu-Quy-Cuu-Tro-Den-Khi-Muoi-Hai-Chu-Ky-Cung-Phan-Chu/story/v3/spoken-narration.txt')
REPORT = PROJECT / "story" / "originality-report.json"
TOKEN_RE = re.compile(r"[\wÀ-ỹĐđ]+", re.UNICODE)

def tokens(text): return [x.casefold() for x in TOKEN_RE.findall(text)]
def digest(text): return hashlib.sha256(text.encode()).hexdigest()

def longest_run(a,b):
    positions=defaultdict(list)
    for j,t in enumerate(b): positions[t].append(j)
    best=0; sample=None
    prev={}
    for i,t in enumerate(a):
        cur={}
        for j in positions.get(t,[]):
            n=prev.get(j-1,0)+1;cur[j]=n
            if n>best: best=n;sample=(i-n+1,i+1)
        prev=cur
    return best, (" ".join(a[sample[0]:sample[1]]) if sample else "")

def main():
    issues=[]
    if not CANDIDATE.exists(): issues.append("candidate_missing")
    if not REFERENCE.exists(): issues.append("reference_missing")
    candidate=CANDIDATE.read_text(encoding='utf-8') if CANDIDATE.exists() else ''
    reference=REFERENCE.read_text(encoding='utf-8') if REFERENCE.exists() else ''
    a,b=tokens(candidate),tokens(reference)
    n=14; ref_shingles={tuple(b[i:i+n]) for i in range(max(0,len(b)-n+1))}; matches=[]
    for i in range(max(0,len(a)-n+1)):
        sh=tuple(a[i:i+n])
        if sh in ref_shingles:
            matches.append({'index':i,'text':' '.join(sh)})
            if len(matches)>=20: break
    if matches: issues.append(f"reference_shingle_matches:{len(matches)}")
    run,sample=longest_run(a,b) if a and b else (0,'')
    if run>=14: issues.append(f"long_exact_token_run:{run}")
    paragraphs=[re.sub(r"\s+"," ",p.strip().casefold()) for p in re.split(r"\n\s*\n",candidate) if len(tokens(p))>=35]
    seen={}; duplicates=[]
    for i,p in enumerate(paragraphs):
        h=hashlib.sha256(p.encode()).hexdigest()
        if h in seen: duplicates.append({'first':seen[h],'duplicate':i})
        else: seen[h]=i
    if duplicates: issues.append(f"internal_duplicate_paragraphs:{len(duplicates)}")
    report={'version':1,'verified':not issues,'status':'passed' if not issues else 'failed','source_canon_sha256':digest(candidate) if candidate else None,'reference_path':str(REFERENCE),'reference_sha256':digest(reference) if reference else None,'shingle_size':n,'shingle_matches':matches,'longest_exact_token_run':run,'longest_run_sample':sample[:500],'internal_duplicate_paragraphs':duplicates,'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'],'longest_run':run,'shingle_matches':len(matches),'duplicate_paragraphs':len(duplicates),'issues':issues},ensure_ascii=False))
    return 0 if report['verified'] else 1

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