import uuid

from django.db import models
from django.contrib.auth.models import BaseUserManager, AbstractUser
from django.contrib.auth import get_user_model


# Create your models here.

class Question(models.Model):
    uid = models.UUIDField(primary_key=True, default=uuid.uuid4, unique=True)
    question_text = models.CharField(max_length=300, unique=True, verbose_name='题目文本')
    pub_date = models.DateTimeField(auto_now=True, verbose_name='发布日期')

    image = models.ImageField(upload_to='images/question_images/', verbose_name='图片', blank=True, null=True)

    choice_a = models.CharField(max_length=300)
    choice_b = models.CharField(max_length=300)
    choice_c = models.CharField(max_length=300)
    choice_d = models.CharField(max_length=300)

    answer = models.CharField(choices=(('A', 'A'), ('B', 'B'), ('C', 'C'), ('D', 'D')), max_length=10)

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


class UserManager(BaseUserManager):  # 自定义Manager管理器
    def _create_user(self, username, password, email, **kwargs):
        if not username:
            raise ValueError("请传入用户名！")
        if not password:
            raise ValueError("请传入密码！")
        if not email:
            raise ValueError("请传入邮箱地址！")
        user = self.model(username=username, email=email, **kwargs)
        user.set_password(password)
        user.save()
        return user

    def create_user(self, username, password, email, **kwargs):  # 创建普通用户
        kwargs['is_superuser'] = False
        return self._create_user(username, password, email, **kwargs)

    def create_superuser(self, username, password, email, **kwargs):  # 创建超级用户
        kwargs['is_superuser'] = True
        kwargs['is_staff'] = True
        return self._create_user(username, password, email, **kwargs)


class CyberUser(AbstractUser):  # 自定义User
    uid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    username = models.CharField(max_length=15, verbose_name="用户名", unique=True)
    nickname = models.CharField(max_length=13, verbose_name="昵称", null=True, blank=True)
    email = models.EmailField(verbose_name="邮箱", null=True, blank=True)
    picture = models.ImageField(upload_to="images/avatars", verbose_name="用户头像", null=True, blank=True)
    date_joined = models.DateTimeField(auto_now_add=True)
    score = models.IntegerField(default=0)

    USERNAME_FIELD = 'username'  # 使用authenticate验证时使用的验证字段，可以换成其他字段，但验证字段必须是唯一的，即设置了unique=True
    REQUIRED_FIELDS = ['email']  # 创建用户时必须填写的字段，除了该列表里的字段还包括password字段以及USERNAME_FIELD中的字段
    EMAIL_FIELD = 'email'  # 发送邮件时使用的字段

    objects = UserManager()

    def get_full_name(self):
        return self.username

    def get_short_name(self):
        return self.username

    class Meta:
        verbose_name = "用户"
        verbose_name_plural = verbose_name


class Choice(models.Model):
    choice = models.CharField(choices=(('A', 'A'), ('B', 'B'), ('C', 'C'), ('D', 'D')), max_length=10)
    question = models.ForeignKey(Question, on_delete=models.DO_NOTHING)
    user = models.ForeignKey(get_user_model(), on_delete=models.DO_NOTHING)
