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

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

def any_of(*terms):
    return any(term.lower() in low for term in terms)

groups = {
    'phantom_vibration': any_of('rung điện thoại ảo', 'rung ảo', 'phantom vibration'),
    'phantom_ringing': any_of('chuông điện thoại ảo', 'phantom ringing', 'nghe chuông dù'),
    'signal_noise': any_of('tín hiệu và nhiễu', 'tín hiệu thật', 'nhiễu nền'),
    'criterion': any_of(
        'ngưỡng quyết định',
        'tiêu chuẩn quyết định',
        'criterion',
        'quyết định này phụ thuộc vào một ngưỡng',
        'nếu ngưỡng cao',
    ),
    'four_outcomes': (
        all(any_of(term) for term in ('phát hiện đúng', 'bỏ sót', 'báo động giả'))
        and any_of('loại bỏ đúng', 'loại trừ đúng', 'correct rejection')
    ),
    'expectation': any_of('kỳ vọng', 'dự đoán', 'mong đợi tín hiệu'),
    'habit_salience': any_of('thói quen kiểm tra', 'kiểm tra liên tục') and any_of('quan trọng', 'đáng chú ý', 'ưu tiên'),
    'tactile_sources': all(any_of(*terms) for terms in [
        ('quần áo cọ', 'vải cọ', 'ma sát quần áo'),
        ('co cơ', 'giật cơ', 'áp lực'),
        ('vị trí điện thoại', 'túi quần', 'sát cơ thể'),
    ]),
    'stress_alertness': any_of('căng thẳng', 'stress') and any_of('cảnh giác', 'tỉnh táo'),
    'prevalence_boundary': any_of(
        'tùy mẫu',
        'phụ thuộc mẫu',
        'khác nhau giữa các nghiên cứu',
        'phụ thuộc cách định nghĩa',
        'các con số về mức độ phổ biến thay đổi mạnh',
        'khác biệt có thể đến từ nhóm người tham gia',
        'tỷ lệ phổ biến thay đổi mạnh',
        'kết quả phụ thuộc vào mẫu nghiên cứu',
    ),
    'diagnostic_boundary': any_of('không phải chẩn đoán', 'không chứng minh', 'không đồng nghĩa') and any_of('ảo giác', 'tâm thần', 'thần kinh'),
    'practical': all(any_of(*terms) for terms in [
        ('đổi kiểu rung', 'đổi mẫu rung'),
        ('tắt rung', 'giảm thông báo rung'),
        ('khỏi sát cơ thể', 'không để sát cơ thể', 'để điện thoại trên bàn', 'đặt máy trên bàn', 'thay vì ép sát cơ thể'),
        ('khung giờ', 'thời điểm cố định', 'theo các khung giờ'),
        ('ghi lại bối cảnh', 'nhật ký một tuần', 'theo dõi một tuần', 'bối cảnh trong một tuần'),
    ]),
    'safety': any_of('đánh giá chuyên môn', 'bác sĩ đánh giá', 'chuyên gia phù hợp đánh giá'),
    'self_diagnosis': any_of(
        'không tự chẩn đoán',
        'đừng tự chẩn đoán',
        'không dùng để tự chẩn đoán',
        'không phải một cách hợp lệ để tự chẩn đoán',
    ),
    'closing_echo': any_of('báo động giả') and any_of('bỏ lỡ tín hiệu', 'bỏ sót tín hiệu'),
}
sources = [name for name in ('Robert Rosenberger', 'Michelle Drouin', 'Michael Rothberg', 'David Laramie') if name.lower() in low]
source_context = {
    'Robert Rosenberger': (
        'robert rosenberger' in low
        and any_of('trải nghiệm công nghệ', 'thói quen với công nghệ', 'quan hệ với thiết bị')
    ),
    'Michelle Drouin': (
        'michelle drouin' in low
        and any_of('rung ảo', 'phantom vibration', 'chuông ảo', 'phantom ringing')
        and any_of('sử dụng điện thoại', 'smartphone', 'thói quen kiểm tra')
    ),
    'Michael Rothberg': (
        'michael rothberg' in low
        and any_of('nhân viên y tế', 'bác sĩ nội trú', 'medical staff', 'bệnh viện')
        and any_of('rung ảo', 'phantom vibration')
    ),
}
forbidden = {p: bool(re.search(rf'(?<![\wÀ-ỹ]){p}(?![\wÀ-ỹ])', low)) for p in ('tôi', 'chúng ta', 'chúng tôi')}
report = {
    'groups': groups,
    'sources': sources,
    'source_count': len(sources),
    'source_context': source_context,
    'forbidden_narrator_pronouns': forbidden,
}
report['status'] = 'PASS' if (
    all(groups.values())
    and len(sources) >= 3
    and all(source_context.values())
    and not any(forbidden.values())
) else 'FAIL'
(root / 'logs/script_semantic_validation.json').write_text(json.dumps(report, ensure_ascii=False, indent=2) + '\n')
print(json.dumps(report, ensure_ascii=False))
raise SystemExit(0 if report['status'] == 'PASS' else 1)
