from django.db import models
from django.core.exceptions import ValidationError
from mediautils.utils import get_upload_path

# On réutilise les icônes définies dans services
from services.models import ICON_CHOICES

def upload_to_home(instance, filename):
    return get_upload_path('home', filename)

class HeroSection(models.Model):
    """
    Section principale (Haut de page).
    Pattern Singleton.
    """
    title = models.CharField("Titre principal", max_length=100, default="Développeur Backend & Architecte Logiciel")
    subtitle = models.CharField("Sous-titre", max_length=200, default="Je conçois des solutions digitales performantes.")
    description = models.TextField("Texte d'accroche", max_length=500)
    
    # Visuel
    illustration = models.ImageField("Image Hero / Avatar", upload_to=upload_to_home, blank=True, null=True)
    
    # Boutons
    btn_primary_text = models.CharField("Bouton 1 (Projets)", max_length=50, default="Voir mes projets")
    btn_secondary_text = models.CharField("Bouton 2 (Contact)", max_length=50, default="Me contacter")

    class Meta:
        verbose_name = "1. Section Hero (En-tête)"
        verbose_name_plural = "1. Section Hero (En-tête)"

    def save(self, *args, **kwargs):
        if not self.pk and HeroSection.objects.exists():
            raise ValidationError("Une seule section Hero autorisée.")
        super().save(*args, **kwargs)
    
    def __str__(self):
        return "Configuration Hero"


class ValueProposition(models.Model):
    """
    Section 'Pourquoi moi ?' / Valeurs ajoutées.
    """
    title = models.CharField("Titre", max_length=50, help_text="ex: Code Propre")
    description = models.CharField("Description courte", max_length=200)
    icon_class = models.CharField("Icône", max_length=50, choices=ICON_CHOICES, default='fa-solid fa-check')
    order = models.PositiveIntegerField(default=0)

    class Meta:
        ordering = ['order']
        verbose_name = "2. Valeur Ajoutée"
        verbose_name_plural = "2. Valeurs Ajoutées"

    def __str__(self):
        return self.title


class Statistic(models.Model):
    """
    Section Chiffres clés.
    """
    number = models.CharField("Chiffre", max_length=20, help_text="ex: 10+, 3, 100%")
    label = models.CharField("Libellé", max_length=50, help_text="ex: Projets réalisés")
    order = models.PositiveIntegerField(default=0)

    class Meta:
        ordering = ['order']
        verbose_name = "3. Statistique"
        verbose_name_plural = "3. Statistiques"

    def __str__(self):
        return f"{self.number} {self.label}"


class CallToAction(models.Model):
    """
    Section finale (Bas de page).
    Pattern Singleton.
    """
    title = models.CharField("Titre", max_length=100, default="Prêt à concrétiser votre projet ?")
    text = models.TextField("Texte incitatif", blank=True)
    btn_text = models.CharField("Texte Bouton", max_length=50, default="Me contacter")

    class Meta:
        verbose_name = "4. Call To Action (Final)"
        verbose_name_plural = "4. Call To Action (Final)"

    def save(self, *args, **kwargs):
        if not self.pk and CallToAction.objects.exists():
            raise ValidationError("Un seul CTA final autorisé.")
        super().save(*args, **kwargs)

    def __str__(self):
        return "Configuration CTA Final"