from django.db import models
from product.models import BaseModel, Product
from decimal import Decimal
from login_signup.models import User

# Create your models here.
class Notification(BaseModel):
    title = models.CharField(max_length=255)
    description = models.TextField()

    class Meta:
        db_table = 'notification' 

    def __str__(self):
        return self.title
    

class Device(BaseModel):

    DEVICE_CHOICES = [
        ('android','Android'),
        ('ios','iOS')
    ]

    device_id = models.CharField(max_length=255, unique=True)
    
    platform = models.CharField(
        max_length=20,
        choices=DEVICE_CHOICES,
        default='android' 
    )
    last_opened_at = models.DateTimeField(null=True, blank=True)
    status = models.BooleanField(default=True)  

    class Meta:
        db_table = 'buyer_device'

    def __str__(self):
        return self.device_id
    
class NotificationHistory(BaseModel):
    notification = models.ForeignKey(Notification, on_delete=models.CASCADE, blank=True, null=True)
    device = models.ForeignKey(Device, null=True, blank=True, on_delete=models.CASCADE)  # For buyer
    vendor = models.ForeignKey(User, null=True, blank=True, on_delete=models.CASCADE)    # For vendor

    title = models.CharField(max_length=255, blank=True, null=True)
    message = models.TextField(blank=True, null=True)

    is_read = models.BooleanField(default=False)
    # read_at = models.DateTimeField(null=True, blank=True)

    sent_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'notification_history'

    def __str__(self):
        title_str = self.title or (self.notification.title if self.notification else "Untitled")
        return f"{title_str} - {self.vendor or self.device}"