from django.core.management.base import BaseCommand
from django.utils.text import slugify
from status.models import Status

class Command(BaseCommand):
    help = 'Loads default statuses into the database'

    def handle(self, *args, **options):
        self.stdout.write('Loading default statuses...')
        statuses = [
            {'title': 'Active', 'type': 'teacher', 'description': ''},
            {'title': 'Inactive', 'type': 'teacher', 'description': ''},
            {'title': 'On Leave', 'type': 'teacher', 'description': ''},
            {'title': 'Active', 'type': 'department', 'description': ''},
            {'title': 'Inactive', 'type': 'department', 'description': ''},
            {'title': 'Active', 'type': 'batch', 'description': ''},
            {'title': 'Passed', 'type': 'batch', 'description': ''},
            {'title': 'Available', 'type': 'class_room', 'description': ''},
            {'title': 'Occupied', 'type': 'class_room', 'description': ''},
            {'title': 'Available', 'type': 'day', 'description': ''},
            {'title': 'Unavailable', 'type': 'day', 'description': ''},
            {'title': 'Weekend', 'type': 'day', 'description': ''},
            {'title': 'Available', 'type': 'time_slot', 'description': ''},
            {'title': 'Unavailable', 'type': 'time_slot', 'description': ''},
            {'title': 'Break', 'type': 'time_slot', 'description': ''},
            {'title': 'Active', 'type': 'course', 'description': ''},
            {'title': 'Inactive', 'type': 'course', 'description': ''},
            {'title': 'Draft', 'type': 'routine', 'description': ''},
            {'title': 'Published', 'type': 'routine', 'description': ''},
            {'title': 'Archived', 'type': 'routine', 'description': ''},
            {'title': 'Available', 'type': 'routine_data', 'description': ''},
            {'title': 'Hidden', 'type': 'routine_data', 'description': ''},
        ]

        for status_data in statuses:
            slug = slugify(f"{status_data['title']}-{status_data['type']}")
            obj, created = Status.objects.get_or_create(
                slug=slug,
                defaults={**status_data, 'is_global': True}
            )
            if created:
                self.stdout.write(self.style.SUCCESS(f"Successfully created status: {obj.title} ({obj.type})"))
            else:
                if not obj.is_global:
                    obj.is_global = True
                    obj.save(update_fields=['is_global'])
                    self.stdout.write(self.style.SUCCESS(f"Marked global: {obj.title} ({obj.type})"))
                else:
                    self.stdout.write(self.style.WARNING(f"Status already exists: {obj.title} ({obj.type})"))

        self.stdout.write(self.style.SUCCESS('Finished loading default statuses.'))
