from django.db import models
from django.contrib.auth.models import User


class Notification(models.Model):
    """
    Notification model to track all notifications for admins.
    """
    NOTIFICATION_TYPES = [
        ('job_posted', 'Job Posted'),
        ('job_application', 'Job Application'),
    ]
    
    recipient = models.ForeignKey(User, on_delete=models.CASCADE, related_name='notifications')
    notification_type = models.CharField(max_length=20, choices=NOTIFICATION_TYPES)
    title = models.CharField(max_length=255)
    message = models.TextField()
    
    # Related objects
    job_id = models.IntegerField(null=True, blank=True)  # Job ID that triggered notification
    application_id = models.IntegerField(null=True, blank=True)  # Application ID that triggered notification
    
    # Status tracking
    is_read = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    
    class Meta:
        ordering = ['-created_at']
        indexes = [
            models.Index(fields=['recipient', '-created_at']),
            models.Index(fields=['is_read']),
        ]
    
    def __str__(self):
        return f"{self.notification_type} - {self.title}"
    
    def mark_as_read(self):
        """Mark notification as read"""
        self.is_read = True
        self.save()
    
    def mark_as_unread(self):
        """Mark notification as unread"""
        self.is_read = False
        self.save()
