from django.db import models
from userapp.models import ParadiseUser


class Bot(models.Model):
    name = models.CharField(max_length=255, verbose_name='机器人姓名')
    description = models.TextField(blank=False, null=False, verbose_name='机器人提示词')

    class Meta:
        verbose_name = '机器人'
        verbose_name_plural = '机器人'

    def __str__(self):
        return self.name

class ChatMessage(models.Model):
    SENDER_USER = 'user'
    SENDER_BOT = 'assistant'
    SENDER_SYSTEM = 'system'

    SENDER_CHOICES = [
        (SENDER_USER, 'user'),
        (SENDER_BOT, 'assistant'),
        (SENDER_SYSTEM, 'system'),
    ]

    MESSAGE_TYPE_TEXT = 'text'
    MESSAGE_TYPE_AUDIO = 'audio'
    MESSAGE_TYPE_VIDEO = 'video'

    MESSAGE_TYPE_CHOICES = [
        (MESSAGE_TYPE_TEXT, 'text'),
        (MESSAGE_TYPE_AUDIO, 'audio'),
        (MESSAGE_TYPE_VIDEO, 'video'),
    ]

    user = models.ForeignKey(ParadiseUser, on_delete=models.CASCADE, verbose_name='用户')
    bot = models.ForeignKey(Bot, on_delete=models.CASCADE, verbose_name='机器人')
    message = models.TextField(max_length=2048, null=False, blank=False, verbose_name='消息内容')
    message_audio_url = models.TextField(max_length=2048, blank=True, verbose_name='消息内容语音链接')
    message_video_url = models.TextField(max_length=2048, blank=True, verbose_name='消息内容视频链接')
    timestamp = models.DateTimeField(auto_now_add=True, verbose_name='时间戳')
    sender = models.CharField(max_length=10, choices=SENDER_CHOICES, verbose_name='发送者')
    message_type = models.CharField(max_length=10, choices=MESSAGE_TYPE_CHOICES, default=MESSAGE_TYPE_TEXT, verbose_name='消息类型')

    class Meta:
        ordering = ['timestamp']  # 按时间顺序排序
        verbose_name = '聊天消息'
        verbose_name_plural = '聊天消息'

    def __str__(self):
        return f"{self.sender}: {self.message[:50]} ({self.timestamp})"