from faker import Faker
import random
from datetime import datetime

# 初始化Faker生成器（简体中文）
fake = Faker("zh_CN")

# 定义固定值
TEAM_ID = 106
PLAN_ID = 131

# 常见民族列表
ETHNIC_GROUPS = [
    "汉",
    "壮",
    "满",
    "回",
    "苗",
    "维吾尔",
    "土家",
    "彝",
    "蒙古",
    "藏",
    "布依",
    "侗",
    "瑶",
    "朝鲜",
    "白",
    "哈尼",
    "哈萨克",
    "黎",
    "傣",
    "畲",
    "傈僳",
    "仡佬",
    "东乡",
    "高山",
    "拉祜",
    "水",
    "佤",
    "纳西",
    "羌",
    "土",
    "仫佬",
    "锡伯",
    "柯尔克孜",
    "达斡尔",
    "景颇",
    "毛南",
    "撒拉",
    "布朗",
    "塔吉克",
    "阿昌",
    "普米",
    "鄂温克",
    "怒",
    "京",
    "基诺",
    "德昂",
    "保安",
    "俄罗斯",
    "裕固",
    "乌兹别克",
    "门巴",
    "鄂伦春",
    "独龙",
    "塔塔尔",
    "赫哲",
    "珞巴",
]

# 疾病列表（健康状态）
HEALTH_CONDITIONS = [
    "无",
    "近视",
    "弱视",
    "鼻炎",
    "哮喘",
    "过敏体质",
    "蛀牙",
    "肥胖",
    "发育迟缓",
    "ADHD",
    "缺铁性贫血",
    "维生素缺乏",
    "易感冒体质",
    "偏瘦",
    "过敏(花粉)",
    "过敏(尘螨)",
    "过敏(牛奶)",
    "消化不良",
    "多动症",
    "抽动症",
    "癫痫",
    "小儿麻痹",
    "乙肝病毒携带",
]

# 艺术特长
ART_TALENTS = [
    "无",
    "钢琴",
    "小提琴",
    "绘画",
    "书法",
    "舞蹈",
    "声乐",
    "戏剧表演",
    "摄影",
    "编程",
    "动画制作",
    "模型制作",
    "手工艺术",
]

# 体育特长
SPORTS_TALENTS = [
    "无",
    "游泳",
    "篮球",
    "足球",
    "乒乓球",
    "羽毛球",
    "田径",
    "武术",
    "跆拳道",
    "空手道",
    "轮滑",
    "滑板",
    "自行车",
    "健美操",
]

# 科技特长
TECH_TALENTS = [
    "无",
    "机器人编程",
    "电子制作",
    "3D打印",
    "人工智能",
    "网页设计",
    "科学实验",
    "天文观测",
    "植物培养",
    "动物观察",
    "航模制作",
]

# 职业列表
OCCUPATIONS = [
    "医生",
    "教师",
    "工程师",
    "程序员",
    "设计师",
    "建筑师",
    "销售经理",
    "会计",
    "律师",
    "公务员",
    "记者",
    "警察",
    "消防员",
    "厨师",
    "商人",
    "自由职业",
    "个体经营者",
    "银行职员",
    "人力资源",
    "市场营销",
]

# 公司类型
WORKPLACES = [
    "国有企业",
    "民营企业",
    "外资企业",
    "合资企业",
    "政府机关",
    "事业单位",
    "学校",
    "医院",
    "设计事务所",
    "律师事务所",
    "科技公司",
    "互联网公司",
    "金融机构",
    "建筑公司",
    "零售企业",
]

# 小学名称
PRIMARY_SCHOOLS = [
    "第一实验小学",
    "第二实验小学",
    "育才小学",
    "阳光小学",
    "希望小学",
    "和平小学",
    "实验小学分校",
    "新世纪小学",
    "东方小学",
    "先锋小学",
    "星光小学",
    "明德小学",
]


def generate_id_num(birthday):
    """生成符合规则的身份证号"""
    # 生成前6位（地区码，使用真实的地区码）
    region_codes = [
        "110101",
        "110105",
        "110106",
        "110107",
        "310112",
        "330102",
        "440106",
        "440304",
    ]

    # 出生日期码（8位）
    birth_code = birthday.replace("-", "")

    # 顺序码（3位）
    sequence_code = str(random.randint(101, 998))

    # 生成校验码（1位）
    base = (
        region_codes[random.randint(0, len(region_codes) - 1)]
        + birth_code
        + sequence_code
    )
    weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
    checksum_map = {
        0: "1",
        1: "0",
        2: "X",
        3: "9",
        4: "8",
        5: "7",
        6: "6",
        7: "5",
        8: "4",
        9: "3",
        10: "2",
    }

    total = 0
    for i in range(17):
        total += int(base[i]) * weight[i]
    checksum = checksum_map[total % 11]

    return base + checksum


def generate_student_data():
    """生成单个学生数据"""
    # 基础信息
    sex = random.choice(["男", "女"])

    # 生日 (2010-2015年出生)
    birthday = fake.date_between(start_date="-15y", end_date="-8y").strftime("%Y-%m-%d")

    # 生成符合规则的身份证号
    id_num = generate_id_num(birthday)

    # 计算年龄（仅用于展示）
    birth_year = int(birthday[:4])
    current_year = datetime.now().year
    age = current_year - birth_year

    # 生成学生信息
    data = {
        "teamId": TEAM_ID,
        "stuName": fake.name(),  # 学生姓名
        "idNum": id_num,  # 身份证号
        "formerName": random.choices(
            ["无", fake.last_name() + fake.first_name()], weights=[0.8, 0.2]
        )[
            0
        ],  # 曾用名
        "sex": sex,  # 性别
        "nation": random.choice(ETHNIC_GROUPS),  # 民族
        "birthday": birthday,  # 生日
        "finishSchool": random.choice(PRIMARY_SCHOOLS),  # 毕业学校
        "file": None,  # 文件
        "hkadr": fake.province()
        + "/"
        + fake.city()
        + "/"
        + fake.district(),  # 户口所在地
        "homeAddress": fake.address(),  # 家庭住址
        "health": random.choice(HEALTH_CONDITIONS),  # 健康状况
        "artisticSpecialty": random.choice(ART_TALENTS),  # 艺术特长
        "artisticAchievements": "无",  # 艺术成就
        "technologicalSpecialty": random.choice(TECH_TALENTS),  # 科技特长
        "technologicalAchievements": "无",  # 科技成就
        "sportsSpecialty": random.choice(SPORTS_TALENTS),  # 体育特长
        "sportsAchievements": "无",  # 体育成就
        "planId": PLAN_ID,  # 计划ID
        "imageUrl": "",  # 图片URL
        "schoolCode": "G" + id_num[:-1],  # 学籍号
        "age": age,  # 年龄（仅用于展示）
        "email": fake.email() if random.random() > 0.7 else "",  # 邮箱
        "height": str(random.randint(130, 170)),  # 身高
        "weight": str(random.randint(25, 60)),  # 体重
        "measurements": f"{random.randint(60, 90)},{random.randint(60, 90)},{random.randint(60, 90)}",  # 三围
    }

    # 生成监护人信息
    guardian_prefix = fake.last_name()

    # 监护人1 - 通常为父母之一
    relationship_choices = ["父亲", "母亲", "爷爷", "奶奶", "外公", "外婆"]
    relationship = random.choice(relationship_choices[:2])  # 主要选择父母

    data.update(
        {
            "guardian1Name": guardian_prefix
            + ("先生" if relationship == "父亲" else "女士"),  # 监护人1姓名
            "guardian1Relationship": relationship,  # 关系
            "guardian1IdCardNumber": generate_id_num(
                fake.date_between(start_date="-55y", end_date="-25y").strftime(
                    "%Y-%m-%d"
                )
            ),  # 身份证号
            "guardian1Contact": fake.phone_number(),  # 联系方式
            "guardian1Occupation": random.choice(OCCUPATIONS),  # 职业
            "guardian1Workplace": random.choice(WORKPLACES),  # 工作单位
        }
    )

    # 监护人2 - 可能是另一位家长或其他人
    if relationship == "父亲":
        guardian2_relationship = "母亲"
    else:
        guardian2_relationship = random.choices(
            ["父亲", "爷爷", "奶奶", "外公", "外婆", "其他亲属"],
            weights=[0.7, 0.1, 0.1, 0.05, 0.05, 0.1],
        )[0]

    data.update(
        {
            "guardian2Name": guardian_prefix
            + (
                "先生" if guardian2_relationship in ["父亲", "爷爷", "外公"] else "女士"
            ),  # 监护人2姓名
            "guardian2Relationship": guardian2_relationship,  # 关系
            "guardian2IdCardNumber": generate_id_num(
                fake.date_between(start_date="-55y", end_date="-25y").strftime(
                    "%Y-%m-%d"
                )
            ),  # 身份证号
            "guardian2Contact": (
                fake.phone_number() if guardian2_relationship != "无" else ""
            ),  # 联系方式
            "guardian2Occupation": (
                random.choice(OCCUPATIONS) if guardian2_relationship != "无" else ""
            ),  # 职业
            "guardian2Workplace": (
                random.choice(WORKPLACES) if guardian2_relationship != "无" else ""
            ),  # 工作单位
        }
    )

    return data


def generate_students():
    student = generate_student_data()
    return student


generate_students()
