from django.db import models
from django.core.exceptions import ValidationError

class ContactPage(models.Model):
    """
    Configuration des textes de la page Contact.
    """
    title = models.CharField("Titre principal", max_length=100, default="Contactez-moi")
    subtitle = models.CharField("Sous-titre", max_length=200, default="Je suis disponible pour vos projets et collaborations.")
    
    # Textes de confirmation
    success_message = models.CharField("Message de succès", max_length=200, default="Merci ! Votre message a bien été envoyé.")
    
    # Meta
    seo_title = models.CharField("Titre SEO", max_length=100, default="Contact - Mahutin Kenny Evra")
    seo_description = models.CharField("Description SEO", max_length=250, blank=True)

    class Meta:
        verbose_name = "Configuration Page Contact"
        verbose_name_plural = "Configuration Page Contact"

    def save(self, *args, **kwargs):
        if not self.pk and ContactPage.objects.exists():
            raise ValidationError("Configuration unique.")
        super().save(*args, **kwargs)

    def __str__(self):
        return "Contenu Page Contact"


class Message(models.Model):
    """
    Archive des messages reçus via le formulaire.
    """
    name = models.CharField("Nom complet", max_length=100)
    email = models.EmailField("Email expéditeur")
    subject = models.CharField("Objet", max_length=200, blank=True)
    content = models.TextField("Message")
    
    # Infos techniques
    created_at = models.DateTimeField("Reçu le", auto_now_add=True)
    ip_address = models.GenericIPAddressField("Adresse IP", blank=True, null=True)
    is_read = models.BooleanField("Lu ?", default=False)

    class Meta:
        ordering = ['-created_at']
        verbose_name = "Message Reçu"
        verbose_name_plural = "Messages Reçus"

    def __str__(self):
        return f"De {self.name} ({self.created_at.strftime('%d/%m/%Y')})"