from apps.core.models import BaseModel
from apps.authentication.models import Organization
from apps.pos_device.models import Device
from django.db import models


class Notification(BaseModel):
    NOTIFICATION_TYPES = (
        ('quota', 'Quota'),
        ('inventory', 'Inventory'),
        ('transaction', 'Transaction'),
        ('system', 'System'),
    )
    device = models.ForeignKey(
        Device,
        on_delete=models.CASCADE,
        related_name='notifications',
        null=True
    )
    organization = models.ForeignKey(
        Organization,
        on_delete=models.CASCADE,
        related_name='notifications',
        null=True
    )

    title = models.CharField(max_length=255, null=True)
    message = models.TextField(null=True, blank=True)
    type = models.CharField(
        max_length=50,
        choices=NOTIFICATION_TYPES,
        default='system'
    )
    delivering_type = models.CharField(max_length=150, null=True, choices=(
        ('general', 'GENERAL'),
        ('private', 'PRIVATE'),
    ), default='private')
    is_read = models.BooleanField(default=False)
    delivered = models.BooleanField(default=False)

    def __str__(self):
        return f'{self.organization.name if self.organization else self.id} - {self.title}'

    def save(self, *args, **kwargs):
        return super(Notification, self).save(*args, **kwargs)
