from django.db import models
from django.contrib.auth.models import User

class Lead(models.Model):
    company = models.CharField(max_length=255)
    contact_name = models.CharField(max_length=255)
    phone = models.CharField(max_length=20)
    notes = models.TextField(blank=True, null=True)
    status = models.CharField(max_length=50, choices=[
        ('New', 'New'),
        ('Contacted', 'Contacted'),
        ('In Progress', 'In Progress'),
        ('Proposal Sent', 'Proposal Sent'),
        ('Closed Won', 'Closed Won'),
        ('Closed Lost', 'Closed Lost'),
        ('No Answer', 'No Answer'),
    ], default='New')
    created_at = models.DateTimeField(auto_now_add=True)
    last_contacted_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.company

class Call(models.Model):
    lead = models.ForeignKey(Lead, related_name='calls', on_delete=models.CASCADE)
    sales_rep = models.ForeignKey(User, on_delete=models.CASCADE)
    call_outcome = models.CharField(max_length=255)
    summary = models.TextField()
    developments = models.TextField()
    requirements_raised = models.TextField()
    actions_taken = models.TextField()
    next_followup_date = models.DateTimeField()
    followup_required = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f"Call with {self.lead.contact_name} on {self.created_at}"