from django.db import models
from django.utils.text import slugify
from django.urls import reverse
from mediautils.utils import upload_to_services

# Liste sélective d'icônes pour développeur Backend / Architecte
ICON_CHOICES = [
    ('fa-solid fa-server', 'Serveur / Backend'),
    ('fa-solid fa-code', 'Code / Développement'),
    ('fa-brands fa-python', 'Python'),
    ('fa-solid fa-database', 'Base de Données'),
    ('fa-solid fa-sitemap', 'Architecture / Structure'),
    ('fa-solid fa-network-wired', 'API / Connexions'),
    ('fa-solid fa-cloud', 'Cloud / Déploiement'),
    ('fa-solid fa-shield-halved', 'Sécurité'),
    ('fa-solid fa-gears', 'Automatisation / DevOps'),
    ('fa-solid fa-laptop-code', 'Application Web'),
    ('fa-solid fa-mobile-screen', 'Mobile / Flutter'),
    ('fa-solid fa-cart-shopping', 'E-commerce'),
    ('fa-solid fa-chart-line', 'Performance / SEO'),
    ('fa-solid fa-bug', 'Debugging / Maintenance'),
    ('fa-solid fa-layer-group', 'Fullstack / Stack'),
    ('fa-solid fa-terminal', 'Terminal / Scripting'),
    ('fa-solid fa-cubes', 'Microservices'),
    ('fa-solid fa-brain', 'IA / Logique'),
    ('fa-solid fa-users-gear', 'CRM / Gestion'),
    ('fa-solid fa-rocket', 'Startup / Lancement'),
]

class Service(models.Model):
    """
    Une offre de service principale.
    """
    title = models.CharField("Titre du service", max_length=100)
    slug = models.SlugField(unique=True, blank=True)
    
    # Contenu
    short_description = models.CharField("Description courte (Carte)", max_length=200, help_text="Affichée sur la grille.")
    full_description = models.TextField("Description complète (Page Détail)", help_text="Supporte le HTML simple.")
    
    # Visuel & Icônes (Liste déroulante)
    icon_class = models.CharField("Icône", max_length=50, choices=ICON_CHOICES, default='fa-solid fa-code')
    
    # Gestion des Images différenciées
    thumbnail = models.ImageField("Image Miniature (Liste)", upload_to=upload_to_services, blank=True, null=True, help_text="Ratio carré ou 4:3 conseillé.")
    banner_image = models.ImageField("Image Bannière (Détail)", upload_to=upload_to_services, blank=True, null=True, help_text="Image large (Hero) pour l'en-tête de la page détail.")
    
    order = models.PositiveIntegerField("Ordre d'affichage", default=0)

    class Meta:
        ordering = ['order']
        verbose_name = "Service"

    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = slugify(self.title)
        super().save(*args, **kwargs)

    def get_absolute_url(self):
        return reverse('services:detail', kwargs={'slug': self.slug})

    def __str__(self):
        return self.title


class ServiceFeature(models.Model):
    """
    Caractéristiques détaillées (Liste à puces dans la page détail).
    """
    service = models.ForeignKey(Service, on_delete=models.CASCADE, related_name='features')
    text = models.CharField("Caractéristique", max_length=200)
    order = models.PositiveIntegerField(default=0)
    
    class Meta:
        ordering = ['order']
        verbose_name = "Feature Service"

    def __str__(self):
        return self.text


class WorkProcess(models.Model):
    """
    Étapes de la méthodologie (Timeline).
    """
    step_number = models.PositiveIntegerField("Numéro", unique=True)
    title = models.CharField("Titre de l'étape", max_length=100)
    description = models.TextField("Description")
    icon_class = models.CharField("Icône", max_length=50, choices=ICON_CHOICES, default='fa-solid fa-code')

    class Meta:
        ordering = ['step_number']
        verbose_name = "Étape Workflow"
        verbose_name_plural = "Workflow / Processus"

    def __str__(self):
        return f"{self.step_number}. {self.title}"