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

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

class AboutProfile(models.Model):
    """
    Configuration de la page À Propos (Bio longue + Photo Portrait).
    Pattern Singleton.
    """
    # Section 1 & 2 : Intro & Portrait
    title = models.CharField("Titre Principal", max_length=100, default="À Propos")
    subtitle = models.CharField("Sous-titre", max_length=200, default="Mon parcours, ma vision et mon approche.")
    
    profile_image = models.ImageField("Photo Portrait (Page About)", upload_to=upload_to_identity, blank=True, null=True, help_text="Différente de la sidebar si souhaité.")
    bio_intro = models.TextField("Bio - Introduction", help_text="Le premier paragraphe accrocheur.")
    bio_content = models.TextField("Bio - Contenu complet", help_text="Le reste de l'histoire.")

    # Section 6 : Intro Vision
    vision_title = models.CharField("Titre Section Vision", max_length=100, default="Ma Vision & Philosophie")
    vision_text = models.TextField("Texte Vision", blank=True)

    class Meta:
        verbose_name = "Page À Propos (Config)"
        verbose_name_plural = "Page À Propos (Config)"

    def save(self, *args, **kwargs):
        if not self.pk and AboutProfile.objects.exists():
            raise ValidationError("Une seule configuration About autorisée.")
        super().save(*args, **kwargs)

    def __str__(self):
        return "Configuration Page About"


class Experience(models.Model):
    """Timeline Professionnelle"""
    job_title = models.CharField("Poste", max_length=100)
    company = models.CharField("Entreprise / Client", max_length=100)
    location = models.CharField("Lieu", max_length=100, blank=True)
    
    start_date = models.DateField("Date de début")
    end_date = models.DateField("Date de fin", null=True, blank=True, help_text="Laisser vide si 'En cours'")
    is_current = models.BooleanField("Poste actuel ?", default=False)
    
    description = models.TextField("Description / Missions", help_text="Liste des tâches et réalisations.")
    
    order = models.PositiveIntegerField(default=0)

    class Meta:
        ordering = ['-start_date'] # Du plus récent au plus ancien
        verbose_name = "Expérience"

    def __str__(self):
        return f"{self.job_title} chez {self.company}"


class Education(models.Model):
    """Formation Académique"""
    degree = models.CharField("Diplôme", max_length=100)
    institution = models.CharField("École / Université", max_length=100)
    year_start = models.CharField("Année Début", max_length=4)
    year_end = models.CharField("Année Fin", max_length=4, blank=True, help_text="Ou vide si en cours")
    
    description = models.TextField("Détails (Optionnel)", blank=True)
    
    order = models.PositiveIntegerField(default=0)

    class Meta:
        ordering = ['-year_start']
        verbose_name = "Formation"

    def __str__(self):
        return self.degree


class Philosophy(models.Model):
    """Points clés de ta vision (Qualité, Performance...)"""
    title = models.CharField("Valeur", 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-code')
    
    order = models.PositiveIntegerField(default=0)

    class Meta:
        ordering = ['order']
        verbose_name = "Valeur / Philosophie"

    def __str__(self):
        return self.title