from django.db import models

from module_iam.models import BaseModel, IAmPrincipal


class InAppNotification(BaseModel):
    user = models.ForeignKey(IAmPrincipal, on_delete=models.CASCADE, related_name='notifications')
    message = models.CharField(max_length=255)
    is_read = models.BooleanField(default=False)

    class Meta:
        db_table = "inapp_notification"
        ordering = ['-created_on']

    def __str__(self):
        return self.message
    
    @classmethod
    def latest_15(cls, user):
        return cls.objects.filter(user=user).order_by('-created_on')[:15]
    
    @classmethod
    def pending_read_count(cls, user):
        return cls.objects.filter(user=user, is_read=False).count()


class PushNotification(BaseModel):
    title = models.CharField(max_length=255)
    banner_image = models.ImageField(upload_to='push_notification_images/', blank=True, null=True)
    message = models.TextField()
    timestamp = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = "push_notification"

    def __str__(self):
        return self.title
