import os
from django.core.management.base import BaseCommand
from colleges.models import College, ProgramsSection, Program
from courses.models import Course

class Command(BaseCommand):
    help = 'Updates the Programs section for Roorkee College of Allied Sciences'

    def handle(self, *args, **options):
        self.stdout.write(self.style.SUCCESS('Starting to update Allied Sciences Programs section'))
        
        # Get the Allied Sciences college
        try:
            allied_college = College.objects.get(id=5)
            self.stdout.write(self.style.SUCCESS(f'Found college: {allied_college.name} (ID: {allied_college.id})'))
        except College.DoesNotExist:
            self.stdout.write(self.style.ERROR('Allied Sciences college with ID 5 not found.'))
            return
        
        # Get or create programs section
        programs_section, created = ProgramsSection.objects.get_or_create(
            college=allied_college,
            defaults={
                'title': 'Our Programs'
            }
        )
        
        if created:
            self.stdout.write(self.style.SUCCESS('Created Programs section'))
        else:
            self.stdout.write(self.style.SUCCESS('Found existing Programs section'))
        
        # Define course data with their program types and order
        course_programs = [
            # Undergraduate Programs
            {'slug': 'bpharm', 'name': 'B.Pharm', 'type': 'UG', 'order': 1},
            {'slug': 'bpharm-le', 'name': 'B.Pharm LE', 'type': 'UG', 'order': 2},
            {'slug': 'bsc-nursing', 'name': 'B.Sc. Nursing', 'type': 'UG', 'order': 3},
            {'slug': 'bsc-microbiology', 'name': 'B.Sc. Microbiology', 'type': 'UG', 'order': 4},
            {'slug': 'dpharm', 'name': 'D.Pharm', 'type': 'DP', 'order': 1},
            
            # Postgraduate Programs
            {'slug': 'mpharm-pharmaceutics', 'name': 'M.Pharm Pharmaceutics', 'type': 'PG', 'order': 1},
            {'slug': 'msc-pharmaceutical-chemistry', 'name': 'M.Sc. Pharmaceutical Chemistry', 'type': 'PG', 'order': 2},
            {'slug': 'msc-microbiology', 'name': 'M.Sc. Microbiology', 'type': 'PG', 'order': 3},
        ]
        
        # Clear existing programs for this section
        programs_section.programs.all().delete()
        self.stdout.write(self.style.SUCCESS('Cleared existing programs'))
        
        # Create new programs
        created_count = 0
        for program_data in course_programs:
            # Check if course exists
            try:
                course = Course.objects.get(slug=program_data['slug'])
                
                # Create program with correct URL format
                program = Program.objects.create(
                    programs_section=programs_section,
                    name=program_data['name'],
                    link=f"/academics/programs/courses/{program_data['slug']}/",
                    program_type=program_data['type'],
                    order=program_data['order']
                )
                
                created_count += 1
                self.stdout.write(self.style.SUCCESS(f"Created program: {program.name} -> {program.link}"))
                
            except Course.DoesNotExist:
                self.stdout.write(self.style.WARNING(f"Course with slug '{program_data['slug']}' not found, skipping"))
        
        self.stdout.write(self.style.SUCCESS(f'Programs section update complete!'))
        self.stdout.write(self.style.SUCCESS(f'Created {created_count} programs'))
        self.stdout.write(self.style.SUCCESS(f'All program URLs use the format: /academics/programs/courses/{{slug}}/'))
        
        # Verify the programs were created
        total_programs = programs_section.programs.count()
        ug_programs = programs_section.programs.filter(program_type='UG').count()
        pg_programs = programs_section.programs.filter(program_type='PG').count()
        dp_programs = programs_section.programs.filter(program_type='DP').count()
        
        self.stdout.write(self.style.SUCCESS(f'Final count - Total: {total_programs}, UG: {ug_programs}, PG: {pg_programs}, Diploma: {dp_programs}'))
