from django.db import models
from django.conf import settings

class Property(models.Model):
    name = models.CharField(max_length=255)
    address = models.TextField(blank=True)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.name

class Guest(models.Model):
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    email = models.EmailField(unique=True)
    phone = models.CharField(max_length=20, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f"{self.first_name} {self.last_name}"

class Stay(models.Model):
    guest = models.ForeignKey(Guest, on_delete=models.CASCADE, related_name='stays')
    property = models.ForeignKey(Property, on_delete=models.CASCADE, related_name='stays')
    check_in = models.DateField()
    check_out = models.DateField()
    total_nights = models.IntegerField(editable=False, default=0)
    created_at = models.DateTimeField(auto_now_add=True)

    def save(self, *args, **kwargs):
        if self.check_in and self.check_out:
            delta = self.check_out - self.check_in
            self.total_nights = delta.days
        super().save(*args, **kwargs)

    def __str__(self):
        return f"{self.guest} at {self.property} ({self.check_in} to {self.check_out})"

class Campaign(models.Model):
    STATUS_CHOICES = [
        ('draft', 'Draft'),
        ('sent', 'Sent'),
    ]
    title = models.CharField(max_length=255)
    subject = models.CharField(max_length=255)
    body = models.TextField()
    status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft')
    sent_at = models.DateTimeField(null=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title