"""Success check shared by both conditions (English edition).

A run directory passes when, for every memo in memos/, transcripts/<same stem>.txt exists, is non-empty, contains at
least 20 English words, and is plausibly the right recording: word-level similarity to the reference script in
experiments_en/corpus/memos.json >= 0.5. The last rule excludes files that hold an error message or raw JSON.
Validated against the known-good ASR output in experiments_en/corpus/transcripts_grok.json and transcripts_compat.json
(run `python check.py --validate`). Usage: python check.py <run_dir>
"""
import difflib
import json
import pathlib
import re
import sys

HERE = pathlib.Path(__file__).resolve().parent
CORPUS = HERE.parent / 'corpus'
REF = {m['id']: m['text'] for m in json.loads((CORPUS / 'memos.json').read_text())['memos']}
WORD = re.compile(r"[A-Za-z]+(?:'[A-Za-z]+)?")


def words(s):
    return [w.lower() for w in WORD.findall(s)]


def similarity(text, ref):
    return round(difflib.SequenceMatcher(None, words(text), words(ref)).ratio(), 3)


def check(run_dir):
    run_dir = pathlib.Path(run_dir)
    memos = sorted((run_dir / 'memos').glob('*.wav'))
    files = {}
    for m in memos:
        t = run_dir / 'transcripts' / f'{m.stem}.txt'
        row = {'exists': t.exists()}
        if t.exists():
            text = t.read_text(encoding='utf-8', errors='replace')
            row.update(chars=len(text.strip()), words=len(words(text)), similarity=similarity(text, REF.get(m.stem, '')),
                       head=text.strip()[:60])
            row['ok'] = row['chars'] > 0 and row['words'] >= 20 and row['similarity'] >= 0.5
        else:
            row['ok'] = False
        files[m.stem] = row
    others = sorted(str(p.relative_to(run_dir)) for p in (run_dir / 'transcripts').rglob('*')) if (run_dir / 'transcripts').exists() else []
    return {'success': bool(files) and all(r['ok'] for r in files.values()),
            'n_ok': sum(r['ok'] for r in files.values()), 'n_memos': len(files),
            'files': files, 'transcripts_dir_listing': others}


def validate():
    """Known-good transcripts must pass; a raw JSON dump and an error string must fail."""
    ids = ['W01', 'W02', 'W05', 'L01', 'I01']
    out = {}
    for name in ['transcripts_grok.json', 'transcripts_compat.json']:
        tr = json.loads((CORPUS / name).read_text())
        out[name] = {i: {'words': len(words(tr[i])), 'similarity': similarity(tr[i], REF[i])} for i in ids}
    raw = json.dumps({'request_id': 'x', 'text': 'So this afternoon I went over the deployment', 'words': [{'start': 0.1, 'end': 0.3, 'text': 'So'}] * 40})
    out['negative_raw_json_W01'] = {'words': len(words(raw)), 'similarity': similarity(raw, REF['W01'])}
    err = "Error transcribing W01.wav: HTTPSConnectionPool(host='api.aibuilder.space', port=443): Max retries exceeded with url: /v1/audio/transcriptions (Caused by NameResolutionError: Failed to resolve)"
    out['negative_error_W01'] = {'words': len(words(err)), 'similarity': similarity(err, REF['W01'])}
    return out


if __name__ == '__main__':
    if sys.argv[1] == '--validate':
        print(json.dumps(validate(), indent=1))
    else:
        print(json.dumps(check(sys.argv[1]), ensure_ascii=False, indent=1))
