import uuid

from django.db import models

from apps.base.models import BaseModel


# class BaseModel(models.Model):
#     """基础模型 Usages: abstract class for Model """
#     id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
#     create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
#     update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
#
#     class Meta:
#         abstract = True

class RoleType(models.TextChoices):
    ADMIN = "admin"
    TEACHER = "teacher"
    TOURIST = "tourist"
    USER = "user"


class AuthType(models.TextChoices):
    ADMIN = "admin"
    SUPERADMIN = "super admin"
    COMMON = "common"
    contest = "contest"


class Gender(models.IntegerChoices):
    UNKNOW = 0, 'Unknown'
    FEMALE = 1, 'Female'
    MALE = 2, 'Male'


class Account(BaseModel):
    name = models.CharField(max_length=20, unique=True)
    real_name = models.CharField(max_length=10, null=True)
    phone = models.CharField(max_length=11, null=True,unique=True)
    password = models.CharField(max_length=20)
    email = models.CharField(max_length=30, unique=True, null=True)
    gender = models.IntegerField(choices=Gender.choices, default=Gender.UNKNOW)
    avatar_url = models.CharField(max_length=255, null=True)
    signature = models.CharField(max_length=255, null=True)
    title_name = models.CharField(max_length=20, null=True)
    is_active = models.BooleanField(default=True)

    # 一个账户有一个角色,one to many
    # role_id = models.ForeignKey("Role", related_name="accounts", on_delete=models.CASCADE)
    def verify_password(self, password: str) -> bool:
        return self.password == password

    class Meta:
        db_table = "account_account"


class Role(BaseModel):
    role = models.CharField(choices=RoleType.choices, max_length=20, default=RoleType.USER)
    description = models.CharField(max_length=255, null=True)
    is_active = models.BooleanField(default=True)
    # 一个角色有多个用户
    accounts = models.ForeignKey(Account, on_delete=models.CASCADE)

    # 一个角色有多个权限，一个权限有多个角色
    # auths = models.ManyToManyField("Auth", related_name="roles")

    class Meta:
        db_table = "account_role"


class Auth(BaseModel):
    type = models.CharField(choices=AuthType.choices, max_length=20, default=AuthType.COMMON)
    permission = models.CharField(max_length=50)
    is_active = models.BooleanField(default=True)
    # 角色权限，多对多
    # roles = models.ManyToManyField(Role, through="RoleAuthLink")
    roles = models.ManyToManyField(Role)

    class Meta:
        db_table = "account_auth"


#
# class AccountRoleLink(BaseModel):
#     account_id = models.ForeignKey(
#         Account, on_delete=models.CASCADE)
#     role_id = models.ForeignKey(
#         Role, on_delete=models.CASCADE)


# class RoleAuthLink(BaseModel):
#     auth_id = models.ForeignKey(
#         Auth, on_delete=models.CASCADE)
#     role_id = models.ForeignKey(
#         Role, on_delete=models.CASCADE)
#
#     class Meta:
#         db_table = "role_link_auth"
