from django.core.management.base import BaseCommand
from django.utils import timezone
from datetime import date, timedelta
from icict_attendance.models import AttendanceDay, AttendanceConfiguration


class Command(BaseCommand):
    help = 'Set up sample attendance days for ICICT AI Workshop'

    def add_arguments(self, parser):
        parser.add_argument(
            '--start-date',
            type=str,
            help='Start date in YYYY-MM-DD format (defaults to today)'
        )

    def handle(self, *args, **options):
        start_date_str = options.get('start_date')
        
        if start_date_str:
            try:
                start_date = date.fromisoformat(start_date_str)
            except ValueError:
                self.stdout.write(
                    self.style.ERROR('Invalid date format. Use YYYY-MM-DD')
                )
                return
        else:
            start_date = date.today()
        
        # Create 6 days: 4 days AI Workshop + 2 days ICICT Conference
        workshop_days = [
            {
                'day_number': 1,
                'title': 'AI Skills Building Workshop - Day 1',
                'description': 'AI Fundamentals & Introduction - Machine Learning basics, AI concepts, and current technology trends.',
                'date': start_date
            },
            {
                'day_number': 2,
                'title': 'AI Skills Building Workshop - Day 2',
                'description': 'Machine Learning Applications - Practical ML applications in various industries and hands-on case studies.',
                'date': start_date + timedelta(days=1)
            },
            {
                'day_number': 3,
                'title': 'AI Skills Building Workshop - Day 3',
                'description': 'AI Tools & Platforms - Overview of AI development tools, frameworks, and hands-on workshops.',
                'date': start_date + timedelta(days=2)
            },
            {
                'day_number': 4,
                'title': 'AI Skills Building Workshop - Day 4',
                'description': 'Advanced AI Techniques - Deep learning, neural networks, and emerging AI technologies.',
                'date': start_date + timedelta(days=3)
            },
            {
                'day_number': 5,
                'title': '7th ICICT 2025 Conference - Day 1',
                'description': '7th ICICT Conference Day 1 - Keynote presentations, research papers, and industry insights on ICT trends.',
                'date': start_date + timedelta(days=4)
            },
            {
                'day_number': 6,
                'title': '7th ICICT 2025 Conference - Day 2',
                'description': '7th ICICT Conference Day 2 - Panel discussions, networking sessions, and future of ICT in Zambia.',
                'date': start_date + timedelta(days=5)
            }
        ]
        
        created_count = 0
        updated_count = 0
        
        for day_info in workshop_days:
            day, created = AttendanceDay.objects.get_or_create(
                day_number=day_info['day_number'],
                defaults={
                    'title': day_info['title'],
                    'description': day_info['description'],
                    'date': day_info['date'],
                    'is_active': True
                }
            )
            
            if created:
                created_count += 1
                self.stdout.write(
                    self.style.SUCCESS(f'Created Day {day.day_number}: {day.title} ({day.date})')
                )
            else:
                # Update existing day
                day.title = day_info['title']
                day.description = day_info['description']
                day.date = day_info['date']
                day.save()
                updated_count += 1
                self.stdout.write(
                    self.style.WARNING(f'Updated Day {day.day_number}: {day.title} ({day.date})')
                )
        
        # Create or update attendance configuration
        config, config_created = AttendanceConfiguration.objects.get_or_create(
            pk=1,
            defaults={
                'current_day': AttendanceDay.objects.get(day_number=1),
                'attendance_enabled': True,
                'welcome_message': 'Welcome to the Artificial Intelligence Skills Building Workshop & 7th ICICT 2025 Conference! Please confirm your attendance by entering your registration ID or email address.'
            }
        )
        
        if config_created:
            self.stdout.write(
                self.style.SUCCESS('Created attendance configuration')
            )
        else:
            self.stdout.write(
                self.style.WARNING('Attendance configuration already exists')
            )
        
        self.stdout.write(
            self.style.SUCCESS(
                f'\nSetup completed:\n'
                f'- Created: {created_count} new attendance days\n'
                f'- Updated: {updated_count} existing attendance days\n'
                f'- Current active day: {config.current_day.title if config.current_day else "None"}\n'
                f'- Attendance enabled: {config.attendance_enabled}'
            )
        )
