from django.db import models
from apps.system.models import CommonAModel
from django.contrib.postgres.fields import JSONField
from utils.model import BaseModel

# Create your models here.
class Questioncat(CommonAModel):
    name = models.CharField(max_length=200, verbose_name='名称')
    parent = models.ForeignKey('self', verbose_name='父', null=True, blank=True, on_delete=models.CASCADE, related_name='questioncat_parent')
    class Meta:
        verbose_name = '题目分类'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name
    
    @property
    def tmtotal(self):
        return self.questioncat.count()
    

class Question(CommonAModel):
    type_choices = (
        ('单选', '单选'),
        ('多选', '多选'),
        ('判断', '判断'),
    )
    level_choices = (
        ('低', '低'),
        ('中', '中'),
        ('高', '高'),
    )
    name = models.TextField(verbose_name='题干')
    img = models.CharField(max_length=1000, null=True, blank=True, verbose_name='题干图片')
    type = models.CharField(max_length=50, default='单选', choices=type_choices, verbose_name='题型')
    level = models.CharField(max_length=50, default='低', choices=level_choices, verbose_name='难度')
    questioncat = models.ForeignKey(Questioncat, blank=True, null=True, on_delete=models.SET_NULL, verbose_name='所属题库', related_name='questioncat')
    options = JSONField(verbose_name='选项')
    right = JSONField(verbose_name='正确答案')
    resolution = models.TextField(verbose_name='解析', blank=True)
    enabled = models.BooleanField('是否启用', default=False)
    year = models.IntegerField('真题年份', null=True, blank=True)
    class Meta:
        verbose_name = '题目'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Paper(CommonAModel):
    name = models.CharField(max_length=200, verbose_name='名称')
    questions = models.ManyToManyField(Question, through='PaperQuestion')
    limit = models.IntegerField(default=0, verbose_name='限时(分钟)')
    total_score = models.FloatField(default=0, verbose_name='满分')
    pass_score = models.FloatField(default=0, verbose_name='通过分数')
    danxuan_count = models.IntegerField(default=0, verbose_name='单选数量')
    danxuan_score = models.FloatField(default=2, verbose_name='单选分数')
    duoxuan_count = models.IntegerField(default=0, verbose_name='多选数量')
    duoxuan_score = models.FloatField(default=4, verbose_name='多选分数')
    panduan_count = models.IntegerField(default=0, verbose_name='判断数量')
    panduan_score = models.FloatField(default=2, verbose_name='判断分数')
    class Meta:
        verbose_name = '押题卷'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class PaperQuestion(BaseModel):
    paper = models.ForeignKey(Paper, on_delete=models.CASCADE, verbose_name='试卷')
    question = models.ForeignKey(Question, on_delete=models.CASCADE, verbose_name='试题')
    total_score = models.FloatField(default=0, verbose_name='单题满分')


class Exam(CommonAModel):
    """
    组织的正式考试
    """
    code = models.CharField('考试编号', max_length=100, null=True, blank=True, unique=True)
    name = models.CharField('名称', max_length=100)
    place = models.CharField('考试地点', max_length=100, null=True, blank=True)
    open_time = models.DateTimeField('开启时间', null=True, blank=True)
    close_time = models.DateTimeField('关闭时间', null=True, blank=True)
    proctor_name = models.CharField('监考人姓名', max_length=100, null=True, blank=True)
    proctor_phone = models.CharField('监考人联系方式', max_length=100, null=True, blank=True)
    chance = models.IntegerField('考试机会', default=3)
    paper = models.ForeignKey(Paper, verbose_name='使用的试卷', on_delete=models.CASCADE, null=True, blank=True)
    is_open = models.BooleanField('是否公开', default=True)

    def __str__(self):
        return self.name

class ExamRecord(CommonAModel):
    '''
    考试记录表
    '''
    type_choices = (
        ('自助模考', '自助模考'),
        ('押卷模考', '押卷模考'),
        ('正式考试', '正式考试')
    )
    name = models.CharField(max_length=200, verbose_name='名称')
    type = models.CharField(max_length=50, default='自助模考',choices = type_choices, verbose_name='考试类型')
    limit = models.IntegerField(default=0, verbose_name='限时(分钟)')
    paper = models.ForeignKey(Paper, on_delete=models.SET_NULL, verbose_name='所用试卷', null=True, blank=True)
    total_score = models.FloatField(default=0, verbose_name='总分')
    score = models.FloatField(default=0, verbose_name='得分')
    took = models.IntegerField(default=0, verbose_name='耗时(秒)')
    start_time = models.DateTimeField(verbose_name='开始答题时间')
    end_time = models.DateTimeField(verbose_name='结束答题时间', null=True, blank=True)
    detail = models.ManyToManyField(Question, verbose_name='答题记录', through='AnswerDetail')
    is_pass = models.BooleanField(default=True, verbose_name='是否通过')
    exam = models.ForeignKey(Exam, verbose_name='关联的正式考试', null=True, blank=True, on_delete= models.SET_NULL)
    is_submited = models.BooleanField(default=False)

    class Meta:
        verbose_name = '考试记录'
        verbose_name_plural = verbose_name



class AnswerDetail(BaseModel):
    examrecord = models.ForeignKey(ExamRecord, on_delete=models.CASCADE)
    total_score = models.FloatField(default=0, verbose_name='该题满分') 
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    user_answer = JSONField(null=True,blank=True)
    score = models.FloatField(default=0, verbose_name='本题得分')
    is_right = models.BooleanField(default=False, verbose_name='是否正确')

    class Meta:
        verbose_name = '答题记录'
        verbose_name_plural = verbose_name
    