#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
个性化回访话术生成工具
根据患者的病历、漏诊项、年龄、性别等信息灵活生成话术
"""

import random
from config import MISSED_DIAGNOSIS_KEY_POINTS

def get_age_group(age):
    """根据年龄判断年龄组"""
    try:
        age_num = int(age)
        if age_num <= 12:
            return "儿童"
        elif age_num <= 17:
            return "青少年"
        elif age_num <= 35:
            return "青年"
        elif age_num <= 59:
            return "中年"
        else:
            return "老年"
    except:
        return "中年"

def generate_risk_description(diagnosis, patient_info=None):
    """
    根据漏诊项目生成个性化的风险描述
    
    Args:
        diagnosis: 漏诊项目
        patient_info: 患者信息字典（可选）
    
    Returns:
        str: 个性化的风险描述
    """
    if diagnosis not in MISSED_DIAGNOSIS_KEY_POINTS:
        return f"如果{diagnosis}问题不及时处理，可能会影响到您的口腔健康"
    
    key_points = MISSED_DIAGNOSIS_KEY_POINTS[diagnosis]
    risk_points = key_points["风险要点"]
    
    # 随机选择2-3个关键风险点
    selected_risks = random.sample(risk_points, min(3, len(risk_points)))
    
    # 构建风险描述
    risk_desc = f"如果{diagnosis}问题不及时处理，可能会出现"
    risk_desc += "、".join(selected_risks[:2])  # 前两个风险点
    if len(selected_risks) > 2:
        risk_desc += f"，甚至{selected_risks[2]}"  # 第三个作为更严重后果
    risk_desc += "的情况"
    
    return risk_desc

def generate_treatment_advantage(diagnosis, patient_info):
    """
    根据漏诊项目和患者信息生成个性化的治疗优势描述
    
    Args:
        diagnosis: 漏诊项目
        patient_info: 患者信息字典，包含年龄、性别等
    
    Returns:
        str: 个性化的治疗优势描述
    """
    if diagnosis not in MISSED_DIAGNOSIS_KEY_POINTS:
        return "及时治疗对您的健康维护会有帮助"
    
    key_points = MISSED_DIAGNOSIS_KEY_POINTS[diagnosis]
    advantage_points = key_points["治疗优势要点"]
    age_adaptability = key_points.get("年龄适应性", {})
    
    # 获取年龄组
    age_group = get_age_group(patient_info.get("年龄", "35"))
    age_specific_msg = age_adaptability.get(age_group, "")
    
    # 选择关键优势点
    selected_advantages = random.sample(advantage_points, min(4, len(advantage_points)))
    
    # 构建治疗优势描述
    advantage_desc = f"您现在{selected_advantages[0]}"
    if len(selected_advantages) > 1:
        advantage_desc += f"，通过治疗可以{selected_advantages[1]}"
    if len(selected_advantages) > 2:
        advantage_desc += f"，{selected_advantages[2]}"
    if len(selected_advantages) > 3:
        advantage_desc += f"，让您{selected_advantages[3]}"
    
    # 添加年龄特异性信息
    if age_specific_msg:
        advantage_desc += f"。{age_specific_msg}"
    
    # 添加同龄患者案例
    gender = patient_info.get("性别", "")
    if gender == "女":
        advantage_desc += f"。很多和您年龄相仿的女性患者，经过治疗后效果都很好"
    elif gender == "男":
        advantage_desc += f"。很多和您年龄相仿的男性患者，经过治疗后都很满意"
    else:
        advantage_desc += f"。很多和您年龄相仿的患者，经过治疗后都有很好的效果"
    
    return advantage_desc

def generate_personalized_callback_points(diagnosis, patient_info):
    """
    生成个性化的回访话术关键点
    
    Args:
        diagnosis: 漏诊项目
        patient_info: 患者信息字典
    
    Returns:
        dict: 包含个性化话术要点的字典
    """
    return {
        "风险描述": generate_risk_description(diagnosis, patient_info),
        "治疗优势": generate_treatment_advantage(diagnosis, patient_info),
        "关键要点": MISSED_DIAGNOSIS_KEY_POINTS.get(diagnosis, {})
    }

# 使用示例
if __name__ == "__main__":
    # 测试不同患者的个性化话术生成
    test_patients = [
        {
            "姓名": "张女士",
            "年龄": "39",
            "性别": "女",
            "漏诊项": "牙槽骨吸收"
        },
        {
            "姓名": "李先生", 
            "年龄": "28",
            "性别": "男",
            "漏诊项": "缺失牙"
        },
        {
            "姓名": "小明",
            "年龄": "8",
            "性别": "男", 
            "漏诊项": "儿牙早矫"
        }
    ]
    
    print("个性化回访话术关键点生成测试:")
    print("=" * 80)
    
    for patient in test_patients:
        print(f"\n患者：{patient['姓名']}（{patient['年龄']}岁 {patient['性别']}）")
        print(f"漏诊项：{patient['漏诊项']}")
        print("-" * 60)
        
        personalized_points = generate_personalized_callback_points(
            patient['漏诊项'], 
            patient
        )
        
        print(f"风险描述：{personalized_points['风险描述']}")
        print(f"治疗优势：{personalized_points['治疗优势']}")
        print()
        
        # 显示可用的关键要点
        key_points = personalized_points['关键要点']
        if key_points:
            print("可用关键要点：")
            print(f"  风险要点：{', '.join(key_points.get('风险要点', []))}")
            print(f"  治疗优势要点：{', '.join(key_points.get('治疗优势要点', []))}")
            if '年龄适应性' in key_points:
                print(f"  年龄适应性：{key_points['年龄适应性']}")
        
        print("=" * 80) 