"""
音乐模块模型
"""
from django.db import models
from apps.users.models import User


class Track(models.Model):
    """音乐曲目"""

    MOOD_CHOICES = [
        ('chill', '放松'),
        ('happy', '开心'),
        ('sleepy', '助眠'),
        ('random', '随机'),
        ('custom', '自定义'),
    ]

    GENERATION_STATUS_CHOICES = [
        ('pending', '待生成'),
        ('generating', '生成中'),
        ('completed', '已完成'),
        ('failed', '失败'),
    ]

    user = models.ForeignKey(
        User, on_delete=models.CASCADE,
        related_name='tracks', verbose_name='用户'
    )
    title = models.CharField('标题', max_length=200)
    lyrics = models.TextField('歌词', blank=True, default='')
    audio_url = models.URLField('音频URL', max_length=500, blank=True, default='')
    opus_url = models.URLField('Opus音频URL', max_length=500, blank=True, default='')
    cover_url = models.URLField('封面URL', max_length=500, blank=True, default='')
    mood = models.CharField(
        '情绪标签', max_length=20,
        choices=MOOD_CHOICES, null=True, blank=True
    )
    duration = models.IntegerField('时长(秒)', default=0)
    prompt = models.TextField('生成提示词', blank=True, default='')
    is_favorite = models.BooleanField('是否收藏', default=False)
    is_default = models.BooleanField('是否默认曲目', default=False)
    intro_opus_data = models.TextField('引导语Opus数据', blank=True, default='')
    generation_status = models.CharField(
        '生成状态', max_length=20,
        choices=GENERATION_STATUS_CHOICES, default='completed'
    )
    created_at = models.DateTimeField('创建时间', auto_now_add=True)
    updated_at = models.DateTimeField('更新时间', auto_now=True)

    class Meta:
        db_table = 'track'
        verbose_name = '音乐曲目'
        verbose_name_plural = verbose_name
        ordering = ['-created_at']
        indexes = [
            models.Index(fields=['user', 'is_favorite']),
            models.Index(fields=['user', 'is_default']),
        ]

    def __str__(self):
        return self.title
