from django.db import models
from common.models.base import TimeStampedModel
from api.property_api.agent.models.agent import Agent
from api.property_api.country.models.country import Country
from api.property_api.city.models.city import City
from api.property_api.state.models.state import State
from django.core.validators import MinValueValidator, MaxValueValidator
from property_categories.models.property_categories import PropertyCategories
from api.property_api.category.models.category import Category

class PropertyListing(models.IntegerChoices):
    Buy = 1
    Rent = 2
    Sale = 3

class Property(TimeStampedModel):
    name = models.CharField(max_length=100)
    address = models.CharField(max_length=250)
    per_sqr_ft_price = models.DecimalField(max_digits=10,decimal_places=2)
    description = models.TextField()
    agent = models.ForeignKey(Agent, on_delete=models.SET_NULL, related_name='agents', blank=True,null=True)
    listed_for = models.IntegerField(choices=PropertyListing)
    country = models.ForeignKey(Country, on_delete=models.CASCADE, related_name="property_country", blank=True, null=True)
    city = models.ForeignKey(City, on_delete=models.CASCADE, related_name="property_city",blank=True,null=True)
    state = models.ForeignKey(State, on_delete=models.CASCADE, related_name="property_state", blank=True, null=True)
    country_name = models.CharField(max_length=120, blank=True, null=True)
    state_name = models.CharField(max_length=120, blank=True, null=True)
    district_name = models.CharField(max_length=120, blank=True, null=True)
    is_related = models.BooleanField(default=False)
    is_flagged = models.BooleanField(default=False)
    contact_number = models.CharField(max_length=15,blank=True,null=True)
    postal_code = models.CharField(max_length=10, blank=True, null=True, db_index=True)
    # property_type = models.ForeignKey(PropertyCategories,on_delete=models.SET_NULL,related_name="proerty_type_category",null=True,blank=True)
    category_type = models.ForeignKey(Category,on_delete=models.SET_NULL,related_name="category",null=True,blank=True)
    is_approved = models.BooleanField(default=False)
    google_map = models.TextField(blank=True,null=True)


    class Meta:
        ordering = ['name']
        verbose_name_plural = "properties"
    
    def __str__(self):
        return self.name
    
    def soft_delete(self):
        if self.is_active:
            self.is_active = False
            self.save(update_fields=['is_active'])


class PropertyReviews(TimeStampedModel):
    review_star = models.PositiveIntegerField(blank=True, validators=[MinValueValidator(1), MaxValueValidator(5)])
    message = models.TextField()
    review_image = models.CharField(max_length=255, blank=True, null=True, help_text="Cloudinary public_id for image")
    review_video = models.CharField(max_length=255, blank=True, null=True, help_text="Cloudinary public_id for video")
    video_duration = models.PositiveIntegerField(blank=True, null=True, help_text="Video duration in seconds")
    property = models.ForeignKey(Property, on_delete=models.CASCADE, related_name='property_review')
    is_flagged_review = models.BooleanField(default=False, blank=True)

    class Meta:
        ordering = ['-created_at']
        verbose_name_plural = 'property_reviews'
        constraints = [
            models.CheckConstraint(
                check=(
                    models.Q(review_image__isnull=False, review_video__isnull=True) |
                    models.Q(review_image__isnull=True, review_video__isnull=False) |
                    models.Q(review_image__isnull=True, review_video__isnull=True)
                ),
                name='property_review_image_or_video_not_both'
            )
        ]

    def soft_delete(self):
        if self.is_active:
            self.is_active = False
            self.save(update_fields=['is_active'])
