from django.db import models

class Business(models.Model):
    INDUSTRY_CHOICES = [
        ('real_estate', 'Real Estate'),
        ('clinic_dental', 'Clinic / Dental'),
        ('salon_spa', 'Salon / Spa'),
        ('coaching_consulting', 'Coaching / Consulting'),
        ('custom', 'Custom (Other)'),
    ]

    business_name = models.CharField(max_length=255)
    industry = models.CharField(max_length=50, choices=INDUSTRY_CHOICES, default='custom')
    phone_number = models.CharField(max_length=20, help_text="Twilio or other provider's phone number")
    agent_name = models.CharField(max_length=100, default='AI Assistant')
    status = models.BooleanField(default=True, help_text="Active or inactive status")

    def __str__(self):
        return self.business_name

    class Meta:
        verbose_name_plural = "Businesses"

class Service(models.Model):
    business = models.ForeignKey(Business, on_delete=models.CASCADE, related_name='services')
    name = models.CharField(max_length=255)
    description = models.TextField(blank=True)
    duration = models.IntegerField(help_text="Duration in minutes")
    price = models.DecimalField(max_digits=10, decimal_places=2)

    def __str__(self):
        return self.name

class Contact(models.Model):
    phone_number = models.CharField(max_length=20, unique=True)
    name = models.CharField(max_length=255, blank=True)

    def __str__(self):
        return self.name or self.phone_number

class Call(models.Model):
    business = models.ForeignKey(Business, on_delete=models.CASCADE, related_name='calls')
    contact = models.ForeignKey(Contact, on_delete=models.CASCADE, related_name='calls')
    start_time = models.DateTimeField(auto_now_add=True)
    end_time = models.DateTimeField(null=True, blank=True)
    summary = models.TextField(blank=True)
    transcript = models.TextField(blank=True)
    conversation_history = models.JSONField(default=list)

    def __str__(self):
        return f"Call with {self.contact} at {self.start_time}"

class Booking(models.Model):
    STATUS_CHOICES = [
        ('scheduled', 'Scheduled'),
        ('completed', 'Completed'),
        ('canceled', 'Canceled'),
    ]

    service = models.ForeignKey(Service, on_delete=models.CASCADE, related_name='bookings')
    contact = models.ForeignKey(Contact, on_delete=models.CASCADE, related_name='bookings')
    start_time = models.DateTimeField()
    end_time = models.DateTimeField()
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='scheduled')

    def __str__(self):
        return f"{self.service} booking for {self.contact} at {self.start_time}"