from django.db import models
from django.contrib.auth import get_user_model
from django.core.validators import MinLengthValidator
from django.utils.translation import gettext_lazy as _

User = get_user_model()

class Survey(models.Model):
    class WaterUsage(models.TextChoices):
        LESS_50 = 'less_50', _('Less than 50 liters')
        BETWEEN_50_100 = '50_100', _('50–100 liters')
        BETWEEN_100_200 = '100_200', _('100–200 liters')
        MORE_200 = 'more_200', _('More than 200 liters')

    class Awareness(models.TextChoices):
        YES = 'yes', _('Yes')
        NO = 'no', _('No')
        NOT_SURE = 'not_sure', _('Not sure')

    class DrippingTap(models.TextChoices):
        LESS_5 = 'less_5', _('Less than 5 liters')
        BETWEEN_5_10 = '5_10', _('5–10 liters')
        BETWEEN_10_20 = '10_20', _('10–20 liters')
        MORE_20 = 'more_20', _('More than 20 liters')

    class Frequency(models.TextChoices):
        ALWAYS = 'always', _('Always')
        SOMETIMES = 'sometimes', _('Sometimes')
        NEVER = 'never', _('Never')

    class WaterSavingAppliances(models.TextChoices):
        LOW_FLOW = 'low_flow', _('Low-flow faucets')
        DUAL_FLUSH = 'dual_flush', _('Dual-flush toilets')
        EFFICIENT_MACHINES = 'efficient_machines', _('Water-efficient washing machines')
        NONE = 'none', _('None')

    # User Information
    user = models.ForeignKey(
        User, 
        on_delete=models.CASCADE,
        related_name='surveys'
    )

    # General Awareness
    water_usage = models.CharField(
        max_length=20,
        choices=WaterUsage.choices,
        verbose_name=_('Daily water usage')
    )
    daily_consumption_awareness = models.CharField(
        max_length=20,
        choices=Awareness.choices,
        verbose_name=_('Aware of daily water consumption')
    )
    dripping_tap_wastage = models.CharField(
        max_length=20,
        choices=DrippingTap.choices,
        verbose_name=_('Estimated dripping tap wastage')
    )

    # Personal Habits
    brushing_habits = models.CharField(
        max_length=20,
        choices=Frequency.choices,
        verbose_name=_('Frequency of turning off tap while brushing')
    )
    fix_leaks = models.CharField(
        max_length=20,
        choices=Awareness.choices,
        verbose_name=_('Promptly fix water leaks')
    )
    reuse_water = models.CharField(
        max_length=20,
        choices=Frequency.choices,
        verbose_name=_('Frequency of water reuse')
    )
    water_saving_appliances = models.CharField(
        max_length=50,
        choices=WaterSavingAppliances.choices,
        verbose_name=_('Water saving appliances used')
    )

    # Feedback and Suggestions
    biggest_cause_of_wastage = models.TextField(
        verbose_name=_('Biggest cause of water wastage'),
        help_text=_('Describe what you think is the biggest cause of water wastage')
    )
    resources_needed = models.TextField(
        verbose_name=_('Resources needed'),
        help_text=_('What resources would help you conserve water better?')
    )
    additional_suggestions = models.TextField(
        blank=True,
        verbose_name=_('Additional suggestions'),
        help_text=_('Any additional suggestions for water conservation')
    )

    # Metadata
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name = _('Water Conservation Survey')
        verbose_name_plural = _('Water Conservation Surveys')
        ordering = ['-created_at']

    def __str__(self):
        return f"Survey by {self.user.username} on {self.created_at.date()}"

    def get_water_consciousness_score(self):
        """
        Calculate a basic water consciousness score based on user responses
        Returns a score between 0-100
        """
        score = 0
        
        # Add points for water-saving habits
        if self.fix_leaks == self.Awareness.YES:
            score += 25
        
        if self.reuse_water == self.Frequency.ALWAYS:
            score += 25
        elif self.reuse_water == self.Frequency.SOMETIMES:
            score += 15
            
        if self.brushing_habits == self.Frequency.ALWAYS:
            score += 25
        elif self.brushing_habits == self.Frequency.SOMETIMES:
            score += 15
            
        if self.water_saving_appliances != self.WaterSavingAppliances.NONE:
            score += 25
            
        return min(score, 100)
