"""
通知模块模型
"""
from django.db import models
from apps.users.models import User


class Notification(models.Model):
    """通知"""

    TYPE_CHOICES = [
        ('system', '系统通知'),
        ('device', '设备通知'),
        ('activity', '活动通知'),
    ]

    user = models.ForeignKey(
        User, on_delete=models.CASCADE,
        related_name='notifications', verbose_name='用户'
    )
    type = models.CharField(
        '通知类型', max_length=20,
        choices=TYPE_CHOICES, default='system'
    )
    title = models.CharField('标题', max_length=200)
    description = models.CharField('摘要', max_length=500, blank=True, default='')
    content = models.TextField('内容', blank=True, default='')
    image_url = models.URLField('图片URL', max_length=500, blank=True, default='')
    is_read = models.BooleanField('是否已读', default=False)
    created_at = models.DateTimeField('创建时间', auto_now_add=True)
    updated_at = models.DateTimeField('更新时间', auto_now=True)

    class Meta:
        db_table = 'notification'
        verbose_name = '通知'
        verbose_name_plural = verbose_name
        ordering = ['-created_at']
        indexes = [
            models.Index(fields=['user', 'is_read']),
            models.Index(fields=['type']),
        ]

    def __str__(self):
        return self.title
