from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
from django.urls import reverse


class PropertyEntry(models.Model):
    class ListingType(models.TextChoices):
        SALE = "sale", "For sale"
        RENTAL = "rental", "For rent"
        UNKNOWN = "unknown", "Not sure"

    class Source(models.TextChoices):
        CURRENT_LOCATION = "current_location", "Current location"
        PHOTO = "photo", "Photo upload"

    source = models.CharField(max_length=32, choices=Source.choices)
    address = models.CharField(max_length=255, blank=True)
    latitude = models.DecimalField(max_digits=9, decimal_places=6, null=True, blank=True)
    longitude = models.DecimalField(max_digits=9, decimal_places=6, null=True, blank=True)
    phone = models.CharField(max_length=40, blank=True)
    email = models.EmailField(blank=True)
    listing_type = models.CharField(max_length=20, choices=ListingType.choices, default=ListingType.UNKNOWN)
    photo = models.ImageField(upload_to="properties/%Y/%m/", blank=True)
    extracted_text = models.TextField(blank=True)
    has_gps_data = models.BooleanField(default=False)
    idealista_url = models.URLField(blank=True)
    is_flagged = models.BooleanField(default=False)
    flag_count = models.PositiveIntegerField(default=0)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ["-created_at"]
        verbose_name_plural = "property entries"

    def __str__(self):
        label = self.address or f"{self.get_source_display()} #{self.pk}"
        return f"{label} ({self.get_listing_type_display()})"

    def get_absolute_url(self):
        return reverse("property_detail", args=[self.pk])

    @property
    def has_location(self):
        return self.latitude is not None and self.longitude is not None


class PropertySuggestion(models.Model):
    property_entry = models.ForeignKey(PropertyEntry, on_delete=models.CASCADE, related_name="suggestions")
    address = models.CharField(max_length=255, blank=True)
    phone = models.CharField(max_length=40, blank=True)
    email = models.EmailField(blank=True)
    listing_type = models.CharField(max_length=20, choices=PropertyEntry.ListingType.choices, blank=True)
    note = models.TextField(blank=True)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ["-created_at"]

    def __str__(self):
        return f"Suggestion for #{self.property_entry_id}"


class PropertyFlag(models.Model):
    property_entry = models.ForeignKey(PropertyEntry, on_delete=models.CASCADE, related_name="flags")
    reason = models.CharField(max_length=160)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ["-created_at"]

    def __str__(self):
        return f"Flag for #{self.property_entry_id}: {self.reason}"
