from django.core.management.base import BaseCommand
from timeslot.models import TimeSlot
from status.models import Status
from django.utils import timezone
from django.template.defaultfilters import slugify

class Command(BaseCommand):
    help = 'Loads default timeslot data into the database.'

    def handle(self, *args, **kwargs):
        default_timeslots = [
            {"start_time": "09:00:00", "end_time": "09:50:00", "slot_name": "1st", "status": "Available"},
            {"start_time": "09:50:00", "end_time": "10:40:00", "slot_name": "2nd", "status": "Available"},
            {"start_time": "10:40:00", "end_time": "11:30:00", "slot_name": "3rd", "status": "Available"},
            {"start_time": "11:30:00", "end_time": "11:45:00", "slot_name": "Break", "status": "Break"},
            {"start_time": "11:45:00", "end_time": "12:35:00", "slot_name": "4th", "status": "Available"},
            {"start_time": "12:35:00", "end_time": "13:25:00", "slot_name": "5th", "status": "Available"},
            {"start_time": "13:25:00", "end_time": "14:30:00", "slot_name": "Lunch Break", "status": "Break"}, # Changed to Lunch Break for clarity
            {"start_time": "14:30:00", "end_time": "15:20:00", "slot_name": "6th", "status": "Available"},
            {"start_time": "15:20:00", "end_time": "16:10:00", "slot_name": "7th", "status": "Available"},
            {"start_time": "16:10:00", "end_time": "17:00:00", "slot_name": "8th", "status": "Available"},
            {"start_time": "17:00:00", "end_time": "17:10:00", "slot_name": "Short Break", "status": "Break"}, # Changed to Short Break for clarity
            {"start_time": "17:10:00", "end_time": "18:00:00", "slot_name": "9th", "status": "Available"},
        ]

        for slot_data in default_timeslots:
            slot_name = slot_data['slot_name']
            start_time = slot_data['start_time']
            end_time = slot_data['end_time']
            status_str = slot_data['status']
            
            status, created = Status.objects.get_or_create(title=status_str, type='time_slot')

            if created:
                self.stdout.write(self.style.SUCCESS(f'Successfully created status: {status_str}'))

            # Check if a timeslot with the same slot_name already exists
            if not TimeSlot.objects.filter(slot_name=slot_name).exists():
                TimeSlot.objects.create(
                    start_time=start_time,
                    end_time=end_time,
                    slot_name=slot_name,
                    status=status,
                    slug=slugify(slot_name) # Ensure slug is generated
                )
                self.stdout.write(self.style.SUCCESS(f'Successfully added timeslot: {slot_name}'))
            else:
                timeslot = TimeSlot.objects.get(slot_name=slot_name)
                if timeslot.status.title != status_str:
                    timeslot.status = status
                    timeslot.save()
                    self.stdout.write(self.style.SUCCESS(f'Successfully updated timeslot: {slot_name}'))
                else:
                    self.stdout.write(self.style.WARNING(f'Timeslot "{slot_name}" already exists with the same status, skipping.'))