from django.db import models

class Parcel(models.Model):
    STATUS_CHOICES = [
        ('pending', 'Pending'),
        ('processed', 'Processed'),
        ('rejected', 'Rejected'),
    ]

    sender_name = models.CharField(max_length=255)
    sender_email = models.EmailField()
    recipient_name = models.CharField(max_length=255)
    recipient_email = models.EmailField()
    tracking_number = models.CharField(max_length=100, unique=True)
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='pending')
    assignee_first_name = models.CharField(max_length=100, blank=True, null=True)
    assignee_last_name = models.CharField(max_length=100, blank=True, null=True)
    received_date = models.DateTimeField()

    def __str__(self):
        return f"Parcel {self.tracking_number} for {self.recipient_name}"

    @property
    def sender_initials(self):
        if self.sender_name:
            parts = self.sender_name.split()
            if len(parts) > 1:
                return (parts[0][0] + parts[-1][0]).upper()
            return parts[0][0].upper()
        return "?"

    @property
    def assignee_initials(self):
        if self.assignee_first_name and self.assignee_last_name:
            return (self.assignee_first_name[0] + self.assignee_last_name[0]).upper()
        return "A"