from django.db import models
from django.utils import timezone


class ContactForm(models.Model):
    """Model to store contact form submissions"""
    
    SUBJECT_CHOICES = [
        ('admission', 'Admission Inquiry'),
        ('academics', 'Academic Information'),
        ('fees', 'Fees & Payments'),
        ('career', 'Career Opportunities'),
        ('other', 'Other'),
    ]
    
    STATUS_CHOICES = [
        ('pending', 'Pending'),
        ('in_progress', 'In Progress'),
        ('resolved', 'Resolved'),
        ('closed', 'Closed'),
    ]
    
    # Required fields
    name = models.CharField(max_length=255, help_text="Full name of the person")
    email = models.EmailField(help_text="Email address for communication")
    subject = models.CharField(
        max_length=20, 
        choices=SUBJECT_CHOICES,
        help_text="Subject/category of the inquiry"
    )
    message = models.TextField(help_text="Detailed message or inquiry")
    
    # Optional fields
    phone = models.CharField(
        max_length=20, 
        blank=True, 
        null=True,
        help_text="Phone number (optional)"
    )
    
    # System fields
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    
    # Status tracking
    status = models.CharField(
        max_length=20,
        choices=STATUS_CHOICES,
        default='pending',
        help_text="Current status of the inquiry"
    )
    
    # Admin notes
    admin_notes = models.TextField(
        blank=True,
        null=True,
        help_text="Internal notes for admin use"
    )
    
    # Response tracking
    responded_at = models.DateTimeField(
        blank=True,
        null=True,
        help_text="When this inquiry was responded to"
    )
    responded_by = models.CharField(
        max_length=255,
        blank=True,
        null=True,
        help_text="Staff member who responded"
    )
    
    class Meta:
        ordering = ['-created_at']  # Show newest first
        verbose_name = "Contact Form Submission"
        verbose_name_plural = "Contact Form Submissions"
    
    def __str__(self):
        return f"{self.name} - {self.get_subject_display()} ({self.created_at.strftime('%Y-%m-%d')})"
    
    def mark_as_responded(self, responded_by=None):
        """Mark this inquiry as responded to"""
        self.status = 'resolved'
        self.responded_at = timezone.now()
        if responded_by:
            self.responded_by = responded_by
        self.save()
    
    def is_recent(self):
        """Check if this inquiry was submitted in the last 24 hours"""
        if not self.created_at:
            return False
        from datetime import timedelta
        return self.created_at >= timezone.now() - timedelta(hours=24)
    
    def get_status_color(self):
        """Return a color code for the status (for admin interface)"""
        colors = {
            'pending': '#f59e0b',  # amber
            'in_progress': '#3b82f6',  # blue
            'resolved': '#10b981',  # green
            'closed': '#6b7280',  # gray
        }
        return colors.get(self.status, '#6b7280')
