from django.core.management.base import BaseCommand
from courses.models import Course, CourseEligibility, CourseFee, FeeDetail, HostelFee

class Command(BaseCommand):
    help = 'Update BCOM eligibility, fee, and hostel fee data.'

    def handle(self, *args, **options):
        course_slug = 'bcom'
        try:
            course = Course.objects.get(slug=course_slug)
        except Course.DoesNotExist:
            self.stdout.write(self.style.ERROR(f'Course with slug "{course_slug}" not found.'))
            return

        # Update eligibility
        eligibility_text = '12th in any stream with at least 50% marks from recognized board'
        try:
            eligibility = CourseEligibility.objects.get(course=course)
            eligibility.academic_qualification = eligibility_text
            eligibility.minimum_marks = '50%'
            eligibility.entrance_exam = 'None'
            eligibility.save()
            self.stdout.write(self.style.SUCCESS('Eligibility updated.'))
        except CourseEligibility.DoesNotExist:
            CourseEligibility.objects.create(
                course=course,
                academic_qualification=eligibility_text,
                minimum_marks='50%',
                entrance_exam='None'
            )
            self.stdout.write(self.style.SUCCESS('Eligibility created.'))

        # Update course fee
        try:
            fee = CourseFee.objects.get(course=course)
        except CourseFee.DoesNotExist:
            fee = CourseFee.objects.create(course=course, domestic='', international='')
            self.stdout.write(self.style.SUCCESS('CourseFee created.'))
        fee.domestic = '67000/- (1st Year), 50000/- (2nd Year), 50000/- (3rd Year)'
        fee.international = 'Contact university for details.'
        fee.save()
        self.stdout.write(self.style.SUCCESS('CourseFee updated.'))

        # Update fee details
        FeeDetail.objects.update_or_create(
            course_fee=fee,
            fee_head='Bachelor of Commerce (B.Com)',
            defaults={
                'first_year': '67000/-',
                'second_year': '50000/-',
                'third_year': '50000/-',
                'fourth_year': '-',
                'is_one_time': False
            }
        )
        self.stdout.write(self.style.SUCCESS('FeeDetail updated.'))

        # Update hostel fee
        notes = (
            'The Hostel fee includes lodging, mess & laundry charges. Student can give maximum of 50 clothes in a month for washing & ironing. '
            'The balance of previous month cannot be carried to the next month. Electricity prepaid meters are installed in each room.'
        )
        HostelFee.objects.update_or_create(
            course=course,
            defaults={
                'fee_per_year': 'Normal Room: 85000/-, Semi Deluxe Room: 95000/-, Deluxe Room: 105000/-',
                'ac_room_additional': '',
                'security_deposit': '',
                'notes': notes
            }
        )
        self.stdout.write(self.style.SUCCESS('HostelFee updated.'))
