from django.db import models
from django.conf import settings


class Notification(models.Model):
    """站内通知 — 异常封禁/额度告警/系统消息等。"""

    TYPE_CHOICES = [
        ('anomaly_disabled_user', '账号因异常被自动封禁'),
        ('anomaly_disabled_team', '团队因异常被自动封禁'),
        ('quota_warning', '额度即将耗尽'),
        ('announcement', '系统公告'),
        ('system', '系统通知'),
    ]

    recipient = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        related_name='notifications',
        verbose_name='接收人',
    )
    type = models.CharField(max_length=30, choices=TYPE_CHOICES, default='system', verbose_name='类型')
    title = models.CharField(max_length=200, blank=True, default='', verbose_name='标题')
    content = models.TextField(blank=True, default='', verbose_name='内容')
    link_url = models.CharField(max_length=500, blank=True, default='', verbose_name='跳转链接')
    is_read = models.BooleanField(default=False, db_index=True, verbose_name='已读')
    created_at = models.DateTimeField(auto_now_add=True, db_index=True, verbose_name='创建时间')

    class Meta:
        verbose_name = '站内通知'
        verbose_name_plural = '站内通知'
        ordering = ['-created_at']
        indexes = [
            models.Index(fields=['recipient', 'is_read', '-created_at']),
        ]

    def __str__(self):
        return f'{self.recipient.username} - {self.title}'
