from typing import Any

from app.services.risk_detector import reinforce_risk


PRIMARY_TOPICS = {
    "亲子关系",
    "婚姻关系",
    "原生家庭",
    "情绪模式",
    "课程学习",
    "个人成长",
    "家族系统",
    "关系内耗",
    "行动力与拖延",
    "其他",
}
COURSE_STAGES = {"觉知", "原生", "合一", "生机", "心光", "大本营综合", "不确定"}
DESENSITIZATION_STATUSES = {"not_needed", "needed", "done", "unknown"}
REVIEW_STATUSES = {"pending", "reviewing", "approved", "needs_revision", "rejected", "forbidden"}


def normalize_ai_item(item: dict[str, Any]) -> dict[str, Any]:
    item = reinforce_risk(dict(item))
    item["primary_topic"] = item.get("primary_topic") if item.get("primary_topic") in PRIMARY_TOPICS else "其他"
    item["course_stage"] = item.get("course_stage") if item.get("course_stage") in COURSE_STAGES else "不确定"
    item["desensitization_status"] = (
        item.get("desensitization_status")
        if item.get("desensitization_status") in DESENSITIZATION_STATUSES
        else "unknown"
    )
    item["suggested_review_status"] = (
        item.get("suggested_review_status") if item.get("suggested_review_status") in REVIEW_STATUSES else "pending"
    )
    for key in ("problem_tags", "audience_tags", "risk_types"):
        if not isinstance(item.get(key), list):
            item[key] = []
    item["suggested_to_standard_qa"] = bool(item.get("suggested_to_standard_qa"))
    item["need_desensitization"] = bool(item.get("need_desensitization"))
    return item

