from django.db import models

# Create your models here.
from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.conf import settings


class Notification(models.Model):
    NOTIFICATION_TYPES = (
        ('property_report', 'Property report'),
        ('service_report', 'Service report'),
        ('producer_report', 'Producer report'),
        ('job_report', 'Job report'),
    )
    
    STATUS_CHOICES = (
        ('unread', 'Unread'),
        ('read', 'Read'),
        ('archived', 'Archived'),
    )
    notification_type = models.CharField(max_length=50, choices=NOTIFICATION_TYPES)
    property_report_id = models.PositiveIntegerField(null=True, blank=True)
    service_report_id = models.PositiveIntegerField(null=True, blank=True)
    producer_report_id = models.PositiveIntegerField(null=True, blank=True)
    job_report_id = models.PositiveIntegerField(null=True, blank=True)
    title = models.CharField(max_length=255)
    message = models.TextField(null=True, blank=True)
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='unread')
    
    
    # Metadata
    created_at = models.DateTimeField(auto_now_add=True)
    read_at = models.DateTimeField(null=True, blank=True)
    
    class Meta:
        ordering = ['-created_at']
        indexes = [
            models.Index(fields=['status']),
            models.Index(fields=['-created_at']),
        ]
    
    def __str__(self):
        return f"{self.notification_type} for {self.recipient.username}"
    
    def mark_as_read(self):
        from django.utils import timezone
        self.status = 'read'
        self.read_at = timezone.now()
        self.save()