#!/usr/bin/env python3
from pathlib import Path
import json
import re

root = Path(__file__).resolve().parents[1]
text = (root / "script/script_contagious_yawning.txt").read_text(encoding="utf-8")
low = text.lower()

groups = {
    "contagious_yawning": ["contagious yawning", "ngáp lây"],
    "spontaneous_distinction": ["spontaneous yawning", "ngáp tự phát"],
    "development": ["phát triển", "trẻ em", "trẻ nhỏ"],
    "social_attention": ["chú ý xã hội", "social attention", "tín hiệu xã hội"],
    "action_perception": ["nhận diện hành động", "action-perception", "quan sát hành động"],
    "arousal": ["tỉnh táo", "arousal", "trạng thái cơ thể"],
    "thermoregulation": ["điều hòa nhiệt", "thermoregulation", "nhiệt độ não"],
    "empathy_limit": ["không phải thước đo", "không thể dùng", "không đủ để đánh giá"],
    "mechanism_uncertainty": ["không có một cơ chế duy nhất", "chưa chứng minh một cơ chế", "không thể kết luận một cơ chế", "không phải lời đáp duy nhất đã được chứng minh"],
    "safety": ["đánh giá chuyên môn", "chuyên gia phù hợp đánh giá", "bác sĩ đánh giá"],
}
moderators = {
    "age": ["tuổi", "độ tuổi"],
    "attention": ["chú ý", "tập trung"],
    "alertness": ["tỉnh táo", "buồn ngủ"],
    "temperature": ["nhiệt độ", "nóng", "mát"],
    "design": ["thiết kế thí nghiệm", "cách thiết kế nghiên cứu", "điều kiện thí nghiệm"],
}
practical = {
    "look_away": ["đổi điểm nhìn", "nhìn sang", "rời mắt"],
    "movement": ["vận động", "đứng dậy", "đi lại"],
    "ventilation": ["thoáng", "không khí mát", "không gian mát"],
    "rest": ["nghỉ", "ngủ"],
    "driving": ["lái xe", "cầm lái"],
}
sources = [name for name in ["Robert Provine", "Andrew Gallup", "Atsushi Senju", "Ivan Norscia", "Elisabetta Palagi"] if name.lower() in low]
forbidden = {
    word: bool(re.search(r"(?<![\wÀ-ỹ])" + re.escape(word) + r"(?![\wÀ-ỹ])", low))
    for word in ["tôi", "chúng ta", "chúng tôi"]
}
report = {
    "groups": {key: any(term.lower() in low for term in terms) for key, terms in groups.items()},
    "moderators": {key: any(term.lower() in low for term in terms) for key, terms in moderators.items()},
    "practical": {key: any(term.lower() in low for term in terms) for key, terms in practical.items()},
    "sources_present": sources,
    "source_count": len(sources),
    "forbidden_narrator_pronouns": forbidden,
    "autism_psychopathy_boundary": (
        any(term in low for term in ["tự kỷ", "autism"])
        and any(term in low for term in ["psychopathy", "thái nhân cách", "rối loạn nhân cách"])
    ),
    "self_diagnosis_boundary": any(term in low for term in ["không tự chẩn đoán", "không đủ để tự chẩn đoán", "đừng tự chẩn đoán"]),
    "driving_safety": any(term in low for term in ["không lái xe", "tránh lái xe", "dừng lái", "không tiếp tục lái", "đừng lái xe"]),
}
report["status"] = "PASS" if (
    all(report["groups"].values())
    and all(report["moderators"].values())
    and all(report["practical"].values())
    and report["source_count"] >= 3
    and not any(forbidden.values())
    and report["autism_psychopathy_boundary"]
    and report["self_diagnosis_boundary"]
    and report["driving_safety"]
) else "FAIL"
(root / "logs/script_semantic_validation.json").write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
print(json.dumps(report, ensure_ascii=False))
raise SystemExit(report["status"] != "PASS")
