from django.db import models
import uuid
from django.utils import timezone

# Create your models here.

# 我们可以在这里添加额外的模型或关系，但现在使用user_management中的现有模型

class OperatorAccount(models.Model):
    """运营账号信息表"""
    
    id = models.AutoField(primary_key=True)
    uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True, verbose_name='UUID')
    
    POSITION_CHOICES = [
        ('editor', '编辑'),
        ('planner', '策划'),
        ('operator', '运营'),
        ('admin', '管理员'),
    ]
    
    username = models.CharField(max_length=100, unique=True, verbose_name='用户名')
    password = models.CharField(max_length=255, verbose_name='密码')
    real_name = models.CharField(max_length=50, verbose_name='真实姓名')
    email = models.EmailField(verbose_name='邮箱')
    phone = models.CharField(max_length=15, verbose_name='电话')
    position = models.CharField(max_length=20, choices=POSITION_CHOICES, verbose_name='工作定位')
    department = models.CharField(max_length=50, verbose_name='部门')
    is_active = models.BooleanField(default=True, verbose_name='是否在职')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    
    class Meta:
        verbose_name = '运营账号'
        verbose_name_plural = '运营账号'
    
    def __str__(self):
        return f"{self.real_name} ({self.username})"

class PlatformAccount(models.Model):
    """平台账号模型"""
    PLATFORM_CHOICES = [
        ('youtube', 'YouTube'),
        ('tiktok', 'TikTok'),
        ('bilibili', 'Bilibili'),
        ('facebook', 'Facebook'),
        ('instagram', 'Instagram'),
        ('twitter', 'Twitter'),
        ('other', '其他平台')
    ]
    
    STATUS_CHOICES = [
        ('active', '正常'),
        ('restricted', '限流'),
        ('suspended', '封禁'),
        ('inactive', '未激活')
    ]
    
    operator = models.ForeignKey(OperatorAccount, on_delete=models.CASCADE, related_name='platform_accounts', verbose_name='运营账号')
    name = models.CharField(max_length=100, verbose_name='账户名称', default='', blank=True)
    platform_name = models.CharField(max_length=20, choices=PLATFORM_CHOICES, verbose_name='平台名称')
    account_name = models.CharField(max_length=100, verbose_name='账号名称')
    account_id = models.CharField(max_length=100, verbose_name='账号ID')
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='active', verbose_name='账号状态')
    followers_count = models.IntegerField(default=0, verbose_name='粉丝数')
    account_url = models.URLField(blank=True, null=True, verbose_name='账号链接')
    description = models.TextField(blank=True, null=True, verbose_name='账号描述')
    tags = models.CharField(max_length=255, blank=True, null=True, verbose_name='标签', help_text='逗号分隔的标签列表')
    profile_image = models.URLField(blank=True, null=True, verbose_name='头像URL')
    last_posting = models.DateTimeField(blank=True, null=True, verbose_name='最后发布时间')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    last_login = models.DateTimeField(blank=True, null=True, verbose_name='最后登录时间')
    
    def __str__(self):
        return f"{self.platform_name} - {self.account_name}"
    
    class Meta:
        verbose_name = '平台账号'
        verbose_name_plural = '平台账号'
        ordering = ['-created_at']
        indexes = [
            models.Index(fields=['platform_name']),
            models.Index(fields=['account_id']),
            models.Index(fields=['status']),
        ]

class Video(models.Model):
    """视频信息表"""
    
    STATUS_CHOICES = [
        ('draft', '草稿'),
        ('scheduled', '已排期'),
        ('published', '已发布'),
        ('failed', '发布失败'),
        ('deleted', '已删除'),
    ]
    
    platform_account = models.ForeignKey(PlatformAccount, on_delete=models.CASCADE, related_name='videos', verbose_name='发布账号')
    title = models.CharField(max_length=200, verbose_name='视频标题')
    description = models.TextField(blank=True, null=True, verbose_name='视频描述')
    video_url = models.URLField(blank=True, null=True, verbose_name='视频地址')
    local_path = models.CharField(max_length=255, blank=True, null=True, verbose_name='本地路径')
    thumbnail_url = models.URLField(blank=True, null=True, verbose_name='缩略图地址')
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='draft', verbose_name='发布状态')
    views_count = models.IntegerField(default=0, verbose_name='播放次数')
    likes_count = models.IntegerField(default=0, verbose_name='点赞数')
    comments_count = models.IntegerField(default=0, verbose_name='评论数')
    shares_count = models.IntegerField(default=0, verbose_name='分享数')
    tags = models.CharField(max_length=500, blank=True, null=True, verbose_name='标签')
    publish_time = models.DateTimeField(blank=True, null=True, verbose_name='发布时间')
    video_id = models.CharField(max_length=100, blank=True, null=True, verbose_name='视频ID')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    
    class Meta:
        verbose_name = '视频'
        verbose_name_plural = '视频'
    
    def __str__(self):
        return f"{self.title} ({self.platform_account.account_name})"
