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

class UserInfo(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='info')  # 关联 User 模型
    nickname = models.CharField(max_length=50, blank=True, null=True)  # 昵称
    phone_number = models.CharField(max_length=20, blank=True, null=True)  # 电话号码
    created_at = models.DateTimeField(auto_now_add=True)  # 注册时间，自动记录
    is_banned = models.BooleanField(default=False)  # 默认用户未封禁

    def __str__(self):
        return self.user.username  # 返回关联的用户名


class OperationRecord(models.Model):
    OPERATION_TYPES = [
        ("IMAGE", "图像检测"),
        ("VIDEO", "视频检测"),
        ("CAMERA", "摄像头检测"),
    ]
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='operations')  # 关联用户
    operation_type = models.CharField(max_length=10, choices=OPERATION_TYPES)  # 操作类型
    description = models.TextField()  # 操作描述
    result_path = models.FileField(max_length=255, null=True, blank=True)  # 检测结果的文件路径
    operation_time = models.DateTimeField(auto_now_add=True)  # 操作时间
    longitude = models.FloatField(null=True, blank=True, verbose_name="经度")
    latitude = models.FloatField(null=True, blank=True, verbose_name="纬度")

    def __str__(self):
        return f"{self.user.username} - {self.operation_type}"


class Department(models.Model):
    title = models.CharField(max_length=16)

class Role(models.Model):
    caption = models.CharField(max_length=16)
    users = models.ManyToManyField(User, related_name='roles')

