from django.db import models

# Create your models here.

from django.contrib.auth.models import AbstractUser
from django.db import models


"""
1. UserProfile（用户扩展信息表）继承AbstractUser
name: 姓名
gender: 性别
ethnicity: 民族
dob: 出生日期
id_card_number: 身份证号码
political_status: 政治面貌

2. ContactInfo（联系方式表）
user: 外键关联到 UserProfile 表。
home_address: 家庭住址
parent_contact: 家长联系电话
student_contact: 本人联系电话
email: 电子邮箱

3. SchoolInfo（就读信息表）
user: 外键关联到 UserProfile 表。
school_name: 学校名称
grade: 年级
class: 班级
admission_date: 入学时间
expected_graduation_date: 预计毕业时间

4. AcademicInfo（学习情况表）
user: 外键关联到 UserProfile 表。
last_semester_score: 上学期期末成绩
this_semester_score: 本学期期中成绩
class_ranking: 班级排名
strong_subject: 特长学科
weak_subject: 薄弱学科

5. FamilyInfo（家庭情况表）
user: 外键关联到 UserProfile 表。
family_member: 家庭成员信息（可以是多个外键或 JSON 字段来存储）
economic_status: 家庭经济状况（是否低保、是否建档立卡等）

6. AwardsPunishments（奖惩情况表）
user: 外键关联到 UserProfile 表。
award_name: 奖项名称
award_date: 奖项颁发时间
award_organization: 奖项颁发单位
discipline_date: 违纪时间
discipline_issue: 违纪事由
discipline_outcome: 处理结果

7. HealthInfo（健康状况表）
user: 外键关联到 UserProfile 表。
height: 身高
weight: 体重
blood_type: 血型
medical_history: 既往病史（重大疾病、过敏史等）
disability_status: 是否残疾
disability_category: 残疾类别
disability_grade: 残疾等级

8. HobbiesInterests（兴趣爱好表）
user: 外键关联到 UserProfile 表。
interests: 兴趣爱好（如阅读、绘画等）
extracurricular_activities: 参加的社团或课外兴趣班

9. SocialPractice（社会实践表）
user: 外键关联到 UserProfile 表。
activity_name: 活动名称
activity_date: 活动时间
activity_location: 活动地点
activity_description: 活动内容
activity_outcome: 活动收获

10. SelfEvaluation（自我评价表）
user: 外键关联到 UserProfile 表。
strengths: 优点
weaknesses: 不足
study_attitude: 学习态度
future_plans: 未来规划
"""


class Subject(models.Model):
    name = models.CharField(max_length=100)  # 科目名称

    def __str__(self):
        return self.name


# EcUser（用户表）
class EcUser(AbstractUser):
    GENDER_CHOICES = [
        ('M', 'Male'),
        ('F', 'Female'),
        ('O', 'Other'),
    ]

    name = models.CharField(max_length=100)
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES, default='M')  # 默认值设置为'M'
    age = models.IntegerField(null=True, blank=True)  # 允许为空，可以为null
    ethnicity = models.CharField(max_length=50, default='Unknown')  # 默认值设置为'Unknown'
    dob = models.DateField(null=True, blank=True)  # 允许为空，可以为null
    id_card_number = models.CharField(max_length=18, unique=True)
    political_status = models.CharField(max_length=50, default='Unknown')  # 默认值设置为'Unknown'

    # 多选字段：开放权限的科目
    subjects = models.ManyToManyField(Subject, related_name='users', blank=True)  # 允许为空，表示可以选择多个科目
    # 使用次数限制
    subject_usage_count = models.IntegerField(default=0)

    def __str__(self):
        return self.name

# ContactInfo（联系方式表）
class ContactInfo(models.Model):
    user = models.OneToOneField(EcUser, on_delete=models.CASCADE)
    home_address = models.CharField(max_length=255)
    parent_contact = models.CharField(max_length=15)
    student_contact = models.CharField(max_length=15)
    email = models.EmailField()

    def __str__(self):
        return f"Contact Info of {self.user.name}"

# SchoolInfo（就读信息表）
class SchoolInfo(models.Model):
    user = models.OneToOneField(EcUser, on_delete=models.CASCADE)
    school_name = models.CharField(max_length=255)
    grade = models.CharField(max_length=50)
    class_name = models.CharField(max_length=50)  # Changed 'class' to 'class_name' to avoid conflict
    admission_date = models.DateField(null=True, blank=True)
    expected_graduation_date = models.DateField(null=True, blank=True)

    def __str__(self):
        return f"School Info of {self.user.name}"

# AcademicInfo（学习情况表）
class AcademicInfo(models.Model):
    user = models.OneToOneField(EcUser, on_delete=models.CASCADE)
    # last_semester_score = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)
    # this_semester_score = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)
    last_semester_score = models.JSONField(null=True, blank=True)
    this_semester_score = models.JSONField(null=True, blank=True)
    
    class_ranking = models.IntegerField(null=True, blank=True)
    strong_subject = models.CharField(max_length=50)
    weak_subject = models.CharField(max_length=50)

    def __str__(self):
        return f"Academic Info of {self.user.name}"

# HealthInfo（健康状况表）
class HealthInfo(models.Model):
    user = models.OneToOneField(EcUser, on_delete=models.CASCADE)
    height = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)
    weight = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)
    blood_type = models.CharField(max_length=3)
    medical_history = models.TextField(null=True, blank=True)
    disability_status = models.BooleanField(null=True, blank=True)
    disability_category = models.CharField(max_length=50, null=True, blank=True)
    disability_grade = models.CharField(max_length=50, null=True, blank=True)

    def __str__(self):
        return f"Health Info of {self.user.name}"

# SelfEvaluation（自我评价表）
class SelfEvaluation(models.Model):
    user = models.OneToOneField(EcUser, on_delete=models.CASCADE)
    strengths = models.TextField()
    weaknesses = models.TextField()
    study_attitude = models.TextField()
    future_plans = models.TextField()

    def __str__(self):
        return f"Self Evaluation of {self.user.name}"



# HobbiesInterests（兴趣爱好表）
class HobbiesInterests(models.Model):
    # 定义一个与EcUser一对多关联的外键
    user = models.ForeignKey(EcUser, on_delete=models.CASCADE)
    # 定义一个文本字段，用于存储用户的兴趣爱好
    interests = models.TextField()
    # 定义一个文本字段，用于存储用户的课外活动
    extracurricular_activities = models.TextField()

    def __str__(self):
        # 返回用户的名字和兴趣爱好
        return f"Hobbies and Interests of {self.user.name}"

# SocialPractice（社会实践表）
class SocialPractice(models.Model):
    # 定义一个与EcUser一对多关联的外键
    user = models.ForeignKey(EcUser, on_delete=models.CASCADE)
    # 定义一个活动名称的字符型字段，最大长度为100
    activity_name = models.CharField(max_length=100)
    # 定义一个活动日期的日期型字段
    activity_date = models.DateField()
    # 定义一个活动地点的字符型字段，最大长度为255
    activity_location = models.CharField(max_length=255)
    # 定义一个活动描述的文本型字段
    activity_description = models.TextField()
    # 定义一个活动结果的文本型字段
    activity_outcome = models.TextField()

    def __str__(self):
        # 返回一个字符串，表示该社交实践的用户名称
        return f"Social Practice of {self.user.name}"

# FamilyInfo（家庭情况表）
class FamilyInfo(models.Model):
    # 定义一个与EcUser一对多关联的外键
    user = models.ForeignKey(EcUser, on_delete=models.CASCADE)
    family_member = models.JSONField(null=True, blank=True)  # Store family member info as JSON or use a related model for complex relationships
    economic_status = models.CharField(max_length=50)

    def __str__(self):
        x = {
            "members": [
                {"relationship": "Father", "name": "John", "age": 45, "occupation": "Engineer"},
                {"relationship": "Mother", "name": "Jane", "age": 43, "occupation": "Doctor"},
                {"relationship": "Brother", "name": "Mike", "age": 20, "occupation": "Student"}
            ]
        }
        return f"Family Info of {self.user.name}"

# AwardsPunishments（奖惩情况表）
class AwardsPunishments(models.Model):
    # 定义一个与EcUser一对多关联的外键
    user = models.ForeignKey(EcUser, on_delete=models.CASCADE)
    award_name = models.CharField(max_length=100)
    award_date = models.DateField()
    award_organization = models.CharField(max_length=100)
    discipline_date = models.DateField(null=True, blank=True)
    discipline_issue = models.CharField(max_length=255, null=True, blank=True)
    discipline_outcome = models.CharField(max_length=255, null=True, blank=True)

    def __str__(self):
        return f"Awards and Punishments of {self.user.name}"
