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

class TimeInfoBase(models.Model):

    created = models.DateTimeField(default=timezone.now)
    last_modified = models.DateTimeField(default=timezone.now)

    class Meta:
        abstract = True

    def save(self, *args, **kwargs):
        if not kwargs.pop("skip_last_modified", False) and not hasattr(self, "skip_last_modified"):
            self.last_modified = timezone.now()
            if kwargs.get("update_fields") is not None:
                kwargs["update_fields"] = list({*kwargs["update_fields"], "last_modified"})

        super().save(*args, **kwargs)

# Create your models here.
class Contact(TimeInfoBase):
    email = models.EmailField(max_length=128)
    name = models.CharField(max_length=128)
    blurb = models.CharField(max_length=254, blank=True)
    subject = models.CharField(max_length=128)
    contacted = models.BooleanField(default=False)

class EmailMessage(TimeInfoBase):
    subject = models.CharField(max_length=255)
    body = models.TextField()
    recipient = models.EmailField()
    sent = models.BooleanField(default=False)

    def __str__(self):
        return self.recipient + " | " + self.subject