# communication/models.py
"""
Models for school communication including SMS and announcements.
"""
from django.db import models
from django.utils.translation import gettext_lazy as _


class SMSMessage(models.Model):
    """
    Represents an SMS message sent to parents or staff.
    
    This model tracks SMS communications, including message type,
    content, recipients, and delivery status. It supports bulk messaging
    to multiple phone numbers.
    
    Attributes:
        message_type (str): Type of message from MESSAGE_TYPE_CHOICES
        message (str): The SMS text content
        phone_numbers (list): JSON list of recipient phone numbers
        status (str): Delivery status from STATUS_CHOICES
        sent_time (datetime): When the message was sent
    """
    
    MESSAGE_TYPE_CHOICES = [
        ('attendance', _('Attendance Alert')),
        ('fee_reminder', _('Fee Reminder')),
        ('exam', _('Exam Notice')),
        ('meeting', _('Parent Meeting')),
        ('emergency', _('Emergency Alert')),
        ('general', _('General Announcement')),
    ]
    
    STATUS_CHOICES = [
        ('draft', _('Draft')),
        ('sent', _('Sent')),
        ('failed', _('Failed')),
    ]
    
    message_type = models.CharField(
        max_length=20, 
        choices=MESSAGE_TYPE_CHOICES
    )
    message = models.TextField()
    phone_numbers = models.JSONField(
        default=list
    )
    status = models.CharField(
        max_length=20, 
        choices=STATUS_CHOICES, 
        default='draft'
    )
    sent_time = models.DateTimeField(
        null=True, 
        blank=True
    )
    
    class Meta:
        verbose_name = "SMS Message"
        verbose_name_plural = "SMS Messages"
        ordering = ['-sent_time']
    
    def __str__(self):
        """Return string representation of the SMS message."""
        if self.sent_time:
            return f"{self.get_message_type_display()} - {self.sent_time.date()}"
        return f"{self.get_message_type_display()} - Draft"
    
    def recipient_count(self):
        """
        Get the number of recipients for this message.
        
        Returns:
            int: Number of phone numbers in the recipients list.
        """
        return len(self.phone_numbers)


class Announcement(models.Model):
    """
    Represents an announcement posted to the school community.
    
    Announcements can be targeted to specific audiences (all, students,
    parents, teachers) and have different priority levels. They can be
    published immediately or saved as drafts.
    
    Attributes:
        title (str): Announcement title
        content (str): Full announcement text
        audience (str): Target audience from AUDIENCE_CHOICES
        priority (str): Priority level from PRIORITY_CHOICES
        publish_date (date): When the announcement should be published
        is_published (bool): Whether the announcement is live
    """
    
    PRIORITY_CHOICES = [
        ('low', _('Low')),
        ('normal', _('Normal')),
        ('high', _('High')),
        ('urgent', _('Urgent')),
    ]
    
    AUDIENCE_CHOICES = [
        ('all', _('Everyone')),
        ('students', _('Students')),
        ('parents', _('Parents')),
        ('teachers', _('Teachers')),
    ]
    
    title = models.CharField(
        max_length=200
    )
    content = models.TextField()
    audience = models.CharField(
        max_length=20, 
        choices=AUDIENCE_CHOICES, 
        default='all'
    )
    priority = models.CharField(
        max_length=20, 
        choices=PRIORITY_CHOICES, 
        default='normal'
    )
    publish_date = models.DateField()
    is_published = models.BooleanField(
        default=True
    )
    
    class Meta:
        ordering = ['-publish_date', '-priority']
        verbose_name = "Announcement"
        verbose_name_plural = "Announcements"
    
    def __str__(self):
        """Return the announcement title."""
        return self.title
    
    def days_until_publish(self):
        """
        Calculate days until the announcement is published.
        
        Returns:
            int: Days until publish date (negative if already published).
        """
        from django.utils import timezone
        delta = self.publish_date - timezone.now().date()
        return delta.days