from django.db import models
from django.urls import reverse

class Deal(models.Model):
    DEAL_TYPES = (
        ('flight', 'Flight'),
        ('points', 'Points/Miles'),
    )
    
    title = models.CharField(max_length=255)
    deal_type = models.CharField(max_length=20, choices=DEAL_TYPES, default='flight')
    origin = models.CharField(max_length=100, blank=True)
    destination = models.CharField(max_length=100)
    current_price = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True)
    original_price = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True)
    points_required = models.PositiveIntegerField(null=True, blank=True)
    description = models.TextField()
    image_url = models.URLField(max_length=500, blank=True)
    is_published = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    source_url = models.URLField(max_length=500, blank=True)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('deal_detail', args=[str(self.id)])

    @property
    def discount_percentage(self):
        if self.current_price and self.original_price:
            return int((1 - (self.current_price / self.original_price)) * 100)
        return None

class Lead(models.Model):
    deal = models.ForeignKey(Deal, on_delete=models.CASCADE, related_name='leads')
    name = models.CharField(max_length=100)
    email = models.EmailField()
    phone = models.CharField(max_length=20, blank=True)
    message = models.TextField(blank=True)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f"Lead from {self.name} for {self.deal.title}"