"""
Crée un exemple de templates pour toute la semaine (Lundi–Vendredi 9h–17h, créneaux 30 min).
À exécuter une fois : python manage.py create_example_templates
Ensuite vous pouvez modifier les templates dans l'admin Django ou l'admin Next.
"""
from django.core.management.base import BaseCommand
from django.utils import timezone

from appointments.models import SlotTemplate


class Command(BaseCommand):
    help = "Crée des templates exemple pour toute la semaine (Lun–Ven 9h–17h, 30 min)"

    def add_arguments(self, parser):
        parser.add_argument(
            "--overwrite",
            action="store_true",
            help="Supprimer les templates existants avant de créer les exemples (défaut: ne pas toucher aux existants)",
        )

    def handle(self, *args, **options):
        if options["overwrite"] and SlotTemplate.objects.exists():
            n = SlotTemplate.objects.count()
            SlotTemplate.objects.all().delete()
            self.stdout.write(self.style.WARNING(f"{n} template(s) supprimé(s)."))

        # Lundi = 0, Mardi = 1, ..., Vendredi = 4
        weekdays = [
            (0, "Lundi"),
            (1, "Mardi"),
            (2, "Mercredi"),
            (3, "Jeudi"),
            (4, "Vendredi"),
        ]
        created = 0
        for weekday, label in weekdays:
            if SlotTemplate.objects.filter(weekday=weekday).exists():
                self.stdout.write(self.style.NOTICE(f"Template {label} existe déjà, ignoré."))
                continue
            SlotTemplate.objects.create(
                weekday=weekday,
                start_time="09:00:00",
                end_time="17:00:00",
                slot_duration_minutes=30,
                is_online=False,
                meet_link="",
                location_notes="Senegal Academy – à préciser",
                is_active=True,
            )
            created += 1
            self.stdout.write(self.style.SUCCESS(f"Template créé : {label} 9h–17h (30 min)."))

        self.stdout.write(self.style.SUCCESS(f"Terminé. {created} template(s) créé(s). Vous pouvez les modifier dans l'admin."))
