import json
from django.core.management.base import BaseCommand
from django.utils.dateparse import parse_datetime
from courses.models import (
    Course, CourseEligibility, CourseFee, FeeDetail, HostelFee, Semester, Subject,
    CareerProspect, Recruiter, WhyJoin, CourseHighlight, CourseImage, Testimonial
)

# Paste your JSON data here
data = {
  "course": {
    "title": "Bachelor of Technology CSE ( AI & ML )",
    "slug": "btech-hons-iot-cyber-security-including-blockchain-technology-data-science",
    "short_description": "A 4-year undergraduate program focusing on Computer Science Engineering with specialization in Artificial Intelligence and Machine Learning.",
    "overview": "This program covers computational logic, reasoning, agent-based models, probabilistic models, perception, cognition, and machine learning. Students learn about state-of-the-art technologies like Deep Learning, Human Computer Interaction, and Augmented Reality.",
    "duration": "4 Years",
    "category": "Engineering",
    "banner_image": "",
    "created_at": "2025-07-25T11:05:00",
    "updated_at": "2025-07-25T11:52:00",
    "eligibility": {
      "academic_qualification": "Passed 10+2 examination with Physics, Chemistry and Mathematics as compulsory subjects from recognized board",
      "minimum_marks": "At least 60% marks",
      "entrance_exam": ""
    },
    "fees": {
      "domestic": "162000 (1st year), 145000 (2nd-4th year)",
      "international": "",
      "details": [
        {
          "fee_head": "Tuition Fee",
          "first_year": "162000",
          "second_year": "145000",
          "third_year": "145000",
          "fourth_year": "145000",
          "is_one_time": False
        }
      ]
    },
    "hostel_fee": {
      "fee_per_year": "85000 (Normal), 95000 (Semi Deluxe), 105000 (Deluxe)",
      "ac_room_additional": "",
      "security_deposit": "",
      "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."
    },
    "semesters": [
      {
        "name": "Semester I",
        "number": 1,
        "subjects": [
          {"code": "24BAS101T", "name": "Engineering Mathematics - I", "credits": 4, "description": ""},
          {"code": "24BAS102P", "name": "Engineering Physics Lab", "credits": 1, "description": ""},
          {"code": "24BAS102T", "name": "Engineering Physics", "credits": 3, "description": ""},
          {"code": "24BCS101P", "name": "Computational Thinking and Programming in C Lab", "credits": 1, "description": ""},
          {"code": "24BCS101T", "name": "Computational Thinking and Programming in C", "credits": 3, "description": ""},
          {"code": "24BME101P", "name": "Concepts of Mechanical Engineering Lab", "credits": 1, "description": ""},
          {"code": "24BME101T", "name": "Concepts of Mechanical Engineering", "credits": 3, "description": ""},
          {"code": "24SEC002P", "name": "Advance Digital Marketing Lab", "credits": 1, "description": ""},
          {"code": "24SEC002T", "name": "Advance Digital Marketing", "credits": 3, "description": ""},
          {"code": "24AEC001T", "name": "Soft Skills and Verbal Communication-I", "credits": 3, "description": ""}
        ]
      },
      {
        "name": "Semester II",
        "number": 2,
        "subjects": [
          {"code": "24BAS201T", "name": "Engineering Mathematics - II", "credits": 4, "description": ""},
          {"code": "24BCE101T", "name": "Energy & Environmental Engineering", "credits": 3, "description": ""},
          {"code": "24BCE101P", "name": "Energy & Environmental Engineering Lab", "credits": 1, "description": ""},
          {"code": "24BCS201P", "name": "Data Structures Lab", "credits": 1, "description": ""},
          {"code": "24BCS201T", "name": "Data Structures", "credits": 4, "description": ""},
          {"code": "24BEC101P", "name": "Principles of Electrical & Electronics Engineering Lab", "credits": 1, "description": ""},
          {"code": "24BEC101T", "name": "Principles of Electrical & Electronics Engineering", "credits": 3, "description": ""},
          {"code": "24SEC009P", "name": "Advance IT Tools for Office Automation Lab", "credits": 1, "description": ""},
          {"code": "24SEC009T", "name": "Advance IT Tools for Office Automation", "credits": 3, "description": ""},
          {"code": "24SEC003T", "name": "Quantitative, Aptitude and Logical Reasoning-I", "credits": 2, "description": ""},
          {"code": "24SYN101P", "name": "Health Wellness (Sports/Yoga/NCC)", "credits": 2, "description": ""}
        ]
      },
      {
        "name": "Semester III",
        "number": 3,
        "subjects": [
          {"code": "24BAS301T", "name": "Discrete Mathematics", "credits": 4, "description": ""},
          {"code": "24BAI301P", "name": "Operating System Lab", "credits": 1, "description": ""},
          {"code": "24BAI301T", "name": "Operating System", "credits": 3, "description": ""},
          {"code": "24BAI302T", "name": "Object Oriented Programming using Java", "credits": 4, "description": ""},
          {"code": "24BAI302P", "name": "Object Oriented Programming using Java Lab", "credits": 1, "description": ""},
          {"code": "24BAI303T", "name": "Introduction to Artificial Intelligence", "credits": 3, "description": ""},
          {"code": "24BAI304P", "name": "Python Programming Lab", "credits": 1, "description": ""},
          {"code": "24BAI304T", "name": "Python Programming", "credits": 3, "description": ""},
          {"code": "24AEC002T", "name": "Soft Skills and Verbal Communication-II", "credits": 3, "description": ""},
          {"code": "24SIP001P", "name": "Summer Internship-I", "credits": 2, "description": ""}
        ]
      },
      {
        "name": "Semester IV",
        "number": 4,
        "subjects": [
          {"code": "24BAI401P", "name": "Design and Analysis of Algorithms Lab", "credits": 1, "description": ""},
          {"code": "24BAI401T", "name": "Design and Analysis of Algorithms", "credits": 4, "description": ""},
          {"code": "24BAI402T", "name": "Time Series Analysis & Forecasting", "credits": 4, "description": ""},
          {"code": "24BAI403T", "name": "Software Engineering", "credits": 3, "description": ""},
          {"code": "24BAI404P", "name": "Machine Learning with Python Lab", "credits": 1, "description": ""},
          {"code": "24BAI404T", "name": "Machine Learning with Python", "credits": 3, "description": ""},
          {"code": "24BAI405P", "name": "Drone Application, Components and Assembly Lab", "credits": 1, "description": ""},
          {"code": "24BAI405T", "name": "Drone Application, Components and Assembly", "credits": 3, "description": ""},
          {"code": "24BAI406P", "name": "Soft Computing Lab", "credits": 1, "description": ""},
          {"code": "24BAI406T", "name": "Soft Computing", "credits": 4, "description": ""},
          {"code": "24SEC004T", "name": "Quantitative, Aptitude and Logical Reasoning-II", "credits": 2, "description": ""}
        ]
      },
      {
        "name": "Semester V",
        "number": 5,
        "subjects": [
          {"code": "24BAI501P", "name": "Database Management System Lab", "credits": 1, "description": ""},
          {"code": "24BAI501T", "name": "Database Management System", "credits": 4, "description": ""},
          {"code": "24BAI502P", "name": "Computer Networks Lab", "credits": 1, "description": ""},
          {"code": "24BAI502T", "name": "Computer Networks", "credits": 4, "description": ""},
          {"code": "24BAI503T", "name": "Computer Organization and Architecture", "credits": 3, "description": ""},
          {"code": "24BAI504P", "name": "Compiler Design Lab", "credits": 1, "description": ""},
          {"code": "24BAI504T", "name": "Compiler Design", "credits": 4, "description": ""},
          {"code": "24EAIXXXT", "name": "Discipline Elective-I", "credits": 3, "description": ""},
          {"code": "24AEC003T", "name": "Soft Skills and Verbal Communication-III", "credits": 3, "description": ""},
          {"code": "24SIP002P", "name": "Summer Internship-II", "credits": 2, "description": ""}
        ]
      },
      {
        "name": "Semester VI",
        "number": 6,
        "subjects": [
          {"code": "24BAI601T", "name": "Cloud Computing", "credits": 4, "description": ""},
          {"code": "24BAI602P", "name": "Data Mining & Warehousing Lab", "credits": 1, "description": ""},
          {"code": "24BAI602T", "name": "Data Mining & Warehousing", "credits": 4, "description": ""},
          {"code": "24BAI603P", "name": "Joy of Deep Learning Lab", "credits": 1, "description": ""},
          {"code": "24BAI603T", "name": "Joy of Deep Learning", "credits": 4, "description": ""},
          {"code": "24BAI604T", "name": "Fundamental of Cyber Security", "credits": 3, "description": ""},
          {"code": "24EAIXXXT", "name": "Discipline Elective-II", "credits": 3, "description": ""},
          {"code": "24EAIXXXT", "name": "Discipline Elective-III", "credits": 3, "description": ""},
          {"code": "24EAI609T", "name": "Natural Language Processing", "credits": 0, "description": ""},
          {"code": "24SEC005T", "name": "Quantitative, Aptitude and Logical Reasoning-III", "credits": 2, "description": ""}
        ]
      },
      {
        "name": "Semester VII",
        "number": 7,
        "subjects": [
          {"code": "24BAI701P", "name": "Cryptography and Network Security Lab", "credits": 1, "description": ""},
          {"code": "24BAI701T", "name": "Cryptography and Network Security", "credits": 4, "description": ""},
          {"code": "24BAI702P", "name": "Big Data Analytics Lab", "credits": 1, "description": ""},
          {"code": "24BAI702T", "name": "Big Data Analytics", "credits": 3, "description": ""},
          {"code": "24EAIXXXT", "name": "Discipline Elective-IV", "credits": 3, "description": ""},
          {"code": "24EAIXXXT", "name": "Discipline Elective-V", "credits": 3, "description": ""},
          {"code": "24OECXXXT", "name": "Open Elective", "credits": 3, "description": ""},
          {"code": "24MDC001T", "name": "Innovation and Entrepreneurship", "credits": 3, "description": ""},
          {"code": "24BAI709P", "name": "Minor Project", "credits": 2, "description": ""},
          {"code": "24SIP003P", "name": "Summer Internship-III", "credits": 2, "description": ""}
        ]
      },
      {
        "name": "Semester VIII",
        "number": 8,
        "subjects": [
          {"code": "24BAI801P", "name": "Major Project", "credits": 12, "description": ""},
          {"code": "24VAC003P", "name": "MOOC/Swayam Online Course of Min 08 Weeks (As decided by department)/Value Added Course run by University - I", "credits": 2, "description": ""},
          {"code": "24VAC004P", "name": "MOOC/Swayam Online Course of Min 08 Weeks (As decided by department)/Value Added Course run by University - II", "credits": 2, "description": ""},
          {"code": "24SEC010P", "name": "Patent/Research Publication/Startup", "credits": 2, "description": ""}
        ]
      }
    ],
    "career_prospects": [
      {"title": "Software Developer/Engineer", "salary_range": "", "description": ""},
      {"title": "Systems Analyst", "salary_range": "", "description": ""},
      {"title": "Network Engineer", "salary_range": "", "description": ""},
      {"title": "Database Administrator", "salary_range": "", "description": ""},
      {"title": "Web Developer", "salary_range": "", "description": ""},
      {"title": "Cyber security Analyst", "salary_range": "", "description": ""},
      {"title": "Cloud Engineer", "salary_range": "", "description": ""},
      {"title": "IT Project Manager", "salary_range": "", "description": ""},
      {"title": "Data Scientist", "salary_range": "", "description": ""}
    ],
    "recruiters": [
      {"name": "TCS", "logo": ""},
      {"name": "Infosys", "logo": ""},
      {"name": "Wipro", "logo": ""}
    ],
    "why_join": [
      {"title": "Cutting-edge Curriculum", "description": "Covers AI, ML, Deep Learning, and modern technologies."},
      {"title": "Industry-Relevant Skills", "description": "Focus on practical and computational skills for the tech industry."}
    ],
    "highlights": [
      {"title": "State-of-the-art Labs", "description": "Modern infrastructure for hands-on learning.", "icon": "lab"},
      {"title": "Expert Faculty", "description": "Learn from experienced professors and industry experts.", "icon": "faculty"}
    ],
    "images": [
      {"image": "", "title": "Campus View", "is_banner": True}
    ],
    "testimonials": [
      {"name": "Amit Sharma", "position": "Software Engineer", "company": "Infosys", "content": "The B.Tech CSE (AI & ML) program gave me the skills and confidence to excel in my career.", "photo": ""}
    ]
  }
}

class Command(BaseCommand):
    help = 'Import B.Tech. Hons. (IOT, Cyber Security including Blockchain Technology Data Science) course data'

    def handle(self, *args, **options):
        c = data['course']
        course, _ = Course.objects.get_or_create(
            slug=c['slug'],
            defaults={
                'title': c['title'],
                'short_description': c['short_description'],
                'overview': c['overview'],
                'duration': c['duration'],
                'category': c['category'],
                'banner_image': c['banner_image'],
                'created_at': parse_datetime(c['created_at']),
                'updated_at': parse_datetime(c['updated_at'])
            }
        )
        # Eligibility
        e = c['eligibility']
        CourseEligibility.objects.update_or_create(
            course=course,
            defaults={
                'academic_qualification': e['academic_qualification'],
                'minimum_marks': e['minimum_marks'],
                'entrance_exam': e['entrance_exam']
            }
        )
        # Fees
        f = c['fees']
        course_fee, _ = CourseFee.objects.update_or_create(
            course=course,
            defaults={
                'domestic': f['domestic'],
                'international': f['international']
            }
        )
        for fd in f['details']:
            FeeDetail.objects.update_or_create(
                course_fee=course_fee,
                fee_head=fd['fee_head'],
                defaults={
                    'first_year': fd['first_year'],
                    'second_year': fd['second_year'],
                    'third_year': fd['third_year'],
                    'fourth_year': fd['fourth_year'],
                    'is_one_time': fd['is_one_time']
                }
            )
        # Hostel Fee
        hf = c['hostel_fee']
        HostelFee.objects.update_or_create(
            course=course,
            defaults={
                'fee_per_year': hf['fee_per_year'],
                'ac_room_additional': hf['ac_room_additional'],
                'security_deposit': hf['security_deposit'],
                'notes': hf['notes']
            }
        )
        # Semesters and Subjects
        for sem in c['semesters']:
            semester, _ = Semester.objects.update_or_create(
                course=course,
                number=sem['number'],
                defaults={'name': sem['name']}
            )
            for subj in sem['subjects']:
                Subject.objects.update_or_create(
                    semester=semester,
                    code=subj['code'],
                    defaults={
                        'name': subj['name'],
                        'credits': subj['credits'],
                        'description': subj['description']
                    }
                )
        # Career Prospects
        for cp in c['career_prospects']:
            CareerProspect.objects.update_or_create(
                course=course,
                title=cp['title'],
                defaults={
                    'salary_range': cp['salary_range'],
                    'description': cp['description']
                }
            )
        # Recruiters
        for r in c['recruiters']:
            Recruiter.objects.update_or_create(
                course=course,
                name=r['name'],
                defaults={'logo': r['logo']}
            )
        # Why Join
        for wj in c['why_join']:
            WhyJoin.objects.update_or_create(
                course=course,
                title=wj['title'],
                defaults={'description': wj['description']}
            )
        # Highlights
        for h in c['highlights']:
            CourseHighlight.objects.update_or_create(
                course=course,
                title=h['title'],
                defaults={
                    'description': h['description'],
                    'icon': h['icon']
                }
            )
        # Images
        for img in c['images']:
            CourseImage.objects.update_or_create(
                course=course,
                title=img['title'],
                defaults={
                    'image': img['image'],
                    'is_banner': img['is_banner']
                }
            )
        # Testimonials
        for t in c['testimonials']:
            Testimonial.objects.update_or_create(
                course=course,
                name=t['name'],
                defaults={
                    'position': t['position'],
                    'company': t['company'],
                    'content': t['content'],
                    'photo': t['photo']
                }
            )
        self.stdout.write(self.style.SUCCESS('Course and related data imported successfully.'))
