from django.db import models
from django.utils.text import slugify

class Policy(models.Model):
    """
    Model to store various policy documents (Privacy, Terms, Support, etc.)
    """
    POLICY_TYPES = [
        ('privacy', 'Privacy Policy'),
        ('terms', 'Terms of Use'),
        ('support', 'Support Policy'),
    ]
    
    type = models.CharField(
        max_length=50,
        choices=POLICY_TYPES,
        unique=True,
        help_text="Type of policy document"
    )
    title = models.CharField(
        max_length=200,
        help_text="Title of the policy"
    )
    slug = models.SlugField(
        max_length=100,
        unique=True,
        blank=True
    )
    description = models.TextField(
        blank=True,
        help_text="Brief description of the policy"
    )
    last_updated = models.DateField(
        auto_now=True,
        help_text="Last update date"
    )
    version = models.CharField(
        max_length=20,
        default="1.0",
        help_text="Policy version number"
    )
    is_active = models.BooleanField(
        default=True,
        help_text="Whether this policy is currently active"
    )
    effective_date = models.DateField(
        help_text="Date when this policy becomes effective"
    )
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    
    class Meta:
        verbose_name = "Policy"
        verbose_name_plural = "Policies"
        ordering = ['type']
    
    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = slugify(self.type)
        super().save(*args, **kwargs)
    
    def __str__(self):
        return f"{self.get_type_display()} (v{self.version})"


class PolicySection(models.Model):
    """
    Individual sections within a policy document
    """
    policy = models.ForeignKey(
        Policy,
        on_delete=models.CASCADE,
        related_name='sections'
    )
    heading = models.CharField(
        max_length=300,
        help_text="Section heading"
    )
    content = models.TextField(
        help_text="Section content"
    )
    order = models.IntegerField(
        default=0,
        help_text="Display order of sections"
    )
    is_active = models.BooleanField(
        default=True,
        help_text="Whether this section is currently active"
    )
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    
    class Meta:
        verbose_name = "Policy Section"
        verbose_name_plural = "Policy Sections"
        ordering = ['policy', 'order']
    
    def __str__(self):
        return f"{self.policy.type} - {self.heading}"

