import argparse, json, re, sys
from docx import Document

def tokenize(text):
    text = re.sub(r"[^a-z0-9+/.\s-]", " ", text.lower())
    return {t for t in re.split(r"\s+", text) if len(t) > 2}

def main():
    p=argparse.ArgumentParser(); p.add_argument('--resume',required=True); p.add_argument('--clusters',required=True); p.add_argument('--phrases',required=True); a=p.parse_args()
    text='\n'.join(x.text for x in Document(a.resume).paragraphs); tokens=tokenize(text)
    clusters=json.load(open(a.clusters)); phrases=json.load(open(a.phrases))
    total=possible=0
    print('Cluster coverage:')
    for name,kws in clusters.items():
        hits=sum(1 for k in kws if k.lower() in tokens); total+=hits; possible+=len(kws); print(f'{name}: {hits}/{len(kws)} ({100*hits/len(kws):.1f}%)')
    overall=100*total/possible; print(f'OVERALL: {total}/{possible} ({overall:.1f}%)')
    hit_phrases=[p for p in phrases if p.lower() in text.lower()]
    missing=[p for p in phrases if p.lower() not in text.lower()]
    print(f'PHRASES: {len(hit_phrases)}/{len(phrases)} ({100*len(hit_phrases)/len(phrases):.1f}%)')
    print('MISSING: ' + ', '.join(missing))
    print('DECISION: ' + ('STRONG' if overall>=75 else 'PROCEED' if overall>=55 else 'BLOCK'))
    sys.exit(2 if overall<55 else 0)
if __name__=='__main__': main()
