from django.db import models
from rbac.models import SoftCommonModel, CommonModel
from django.contrib.postgres.fields import JSONField, ArrayField
# Create your models here.


class Questioncat(CommonModel):
    type_choices = (
        ('公共', '公共'),
        ('专业', '专业'),
    )
    name = models.CharField(max_length=200, verbose_name='名称')
    pid = models.ForeignKey('self', verbose_name='父', null=True, blank=True, on_delete=models.CASCADE, related_name='questioncatpid')
    is_subject = models.BooleanField(default=False, verbose_name='是否是学科')
    type = models.CharField(max_length=50, default='专业', choices=type_choices, verbose_name='科目类型')
    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(SoftCommonModel):
    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)

    class Meta:
        verbose_name = '题目'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name