from django.core.management.base import BaseCommand
from apps.organizations.models import LegacyLocation, LegacyMainSection, LegacySubSection


class Command(BaseCommand):
    help = "Populates Locations, MainSections, and SubSections"

    def handle(self, *args, **options):
        # 1. Define Locations
        locations_data = [
            {"id": 48, "name_ar": "التنويم", "name_en": "Inpatient"},
            {"id": 49, "name_ar": "العيادات الخارجية", "name_en": "Outpatient Clinics"},
            {"id": 82, "name_ar": "الطوارئ", "name_en": "Emergency"},
            {"id": 110, "name_ar": "اخرى", "name_en": "Others"},
        ]
        # 2. Define Main Sections
        main_sections_data = [
            {"id": 1, "name_ar": "الطبي", "name_en": "Medical"},
            {"id": 2, "name_ar": "التمريض", "name_en": "Nursing"},
            {"id": 3, "name_ar": "الخدمات المساندة", "name_en": "Support Services"},
            {"id": 4, "name_ar": "الإداري", "name_en": "Administrative"},
            {"id": 5, "name_ar": "IT", "name_en": "Information Technology"},
        ]

        # 3. Define the massive SubSection list (Sample of the 156 items)
        subsections_data = [
            {
                "id": "48",
                "name_ar": "تنويم الأطفال",
                "name_en": "Pediatric Ward",
                "location_id": "48",
                "main_section_id": "1",
            },
            {
                "id": "46",
                "name_ar": "تنويم الباطنية",
                "name_en": "Internal Medicine Ward",
                "location_id": "48",
                "main_section_id": "1",
            },
            {
                "id": "45",
                "name_ar": "تنويم الجراحة العامة",
                "name_en": "General Surgery Ward",
                "location_id": "48",
                "main_section_id": "1",
            },
            {
                "id": "47",
                "name_ar": "تنويم النساء والولادة",
                "name_en": "OB/GYN Ward",
                "location_id": "48",
                "main_section_id": "1",
            },
            {
                "id": "52",
                "name_ar": "قسم الأشعة",
                "name_en": "Radiology Department",
                "location_id": "48",
                "main_section_id": "1",
            },
            {
                "id": "43",
                "name_ar": "قسم التخدير",
                "name_en": "Anesthesia Department",
                "location_id": "48",
                "main_section_id": "1",
            },
            {
                "id": "55",
                "name_ar": "قسم التغذية",
                "name_en": "Nutrition Department",
                "location_id": "48",
                "main_section_id": "1",
            },
            {
                "id": "208",
                "name_ar": "قسم الجهاز الهمضي و الكبد والمناظير",
                "name_en": "Gastroenterology, Hepatology & Endoscopy",
                "location_id": "48",
                "main_section_id": "1",
            },
            {
                "id": "42",
                "name_ar": "قسم الحضانة",
                "name_en": "Nursery Department",
                "location_id": "48",
                "main_section_id": "1",
            },
            {
                "id": "51",
                "name_ar": "قسم العلاج التنفسي",
                "name_en": "Respiratory Therapy Department",
                "location_id": "48",
                "main_section_id": "1",
            },
            {
                "id": "50",
                "name_ar": "قسم العلاج الطبيعي",
                "name_en": "Physiotherapy Department",
                "location_id": "48",
                "main_section_id": "1",
            },
            {
                "id": "44",
                "name_ar": "قسم العمليات",
                "name_en": "OR Department",
                "location_id": "48",
                "main_section_id": "1",
            },
            {
                "id": "212",
                "name_ar": "قسم جراحة المخ والاعصاب",
                "name_en": "Neurosurgery Department",
                "location_id": "48",
                "main_section_id": "1",
            },
            {
                "id": "176",
                "name_ar": "قسم عمليات الولادة",
                "name_en": "Labor & Delivery OR",
                "location_id": "48",
                "main_section_id": "1",
            },
            {
                "id": "216",
                "name_ar": "وحدة العناية القلبيه",
                "name_en": "Coronary Care Unit (CCU)",
                "location_id": "48",
                "main_section_id": "1",
            },
            {
                "id": "38",
                "name_ar": "وحدة العناية المتوسطة",
                "name_en": "Intermediate Care Unit",
                "location_id": "48",
                "main_section_id": "1",
            },
            {
                "id": "39",
                "name_ar": "وحدة العناية المتوسطة للأطفال",
                "name_en": "Pediatric Intermediate Care Unit",
                "location_id": "48",
                "main_section_id": "1",
            },
            {
                "id": "35",
                "name_ar": "وحدة العناية المركزة",
                "name_en": "Intensive Care Unit (ICU)",
                "location_id": "48",
                "main_section_id": "1",
            },
            {
                "id": "37",
                "name_ar": "وحدة العناية المركزة لحديثي الولادة",
                "name_en": "Neonatal Intensive Care Unit (NICU)",
                "location_id": "48",
                "main_section_id": "1",
            },
            {
                "id": "36",
                "name_ar": "وحدة العناية المركزة للأطفال",
                "name_en": "Pediatric Intensive Care Unit (PICU)",
                "location_id": "48",
                "main_section_id": "1",
            },
            {
                "id": "147",
                "name_ar": "وحدة المناظير",
                "name_en": "Endoscopy Unit",
                "location_id": "48",
                "main_section_id": "1",
            },
            {
                "id": "41",
                "name_ar": "وحدة غسيل الكلى",
                "name_en": "Hemodialysis Unit",
                "location_id": "48",
                "main_section_id": "1",
            },
            {
                "id": "40",
                "name_ar": "وحدة مرضى طويلي الإقامة",
                "name_en": "Long-term Care Unit",
                "location_id": "48",
                "main_section_id": "1",
            },
            {
                "id": "155",
                "name_ar": "تمريض الحضانة",
                "name_en": "Nursery Nursing",
                "location_id": "48",
                "main_section_id": "2",
            },
            {
                "id": "151",
                "name_ar": "تمريض تنويم الأطفال",
                "name_en": "Pediatric Ward Nursing",
                "location_id": "48",
                "main_section_id": "2",
            },
            {
                "id": "152",
                "name_ar": "تمريض تنويم الباطنية",
                "name_en": "Internal Medicine Ward Nursing",
                "location_id": "48",
                "main_section_id": "2",
            },
            {
                "id": "153",
                "name_ar": "تمريض تنويم الجراحة العامة",
                "name_en": "General Surgery Ward Nursing",
                "location_id": "48",
                "main_section_id": "2",
            },
            {
                "id": "154",
                "name_ar": "تمريض تنويم النساء والولادة",
                "name_en": "OB/GYN Ward Nursing",
                "location_id": "48",
                "main_section_id": "2",
            },
            {
                "id": "177",
                "name_ar": "تمريض قسم الإفاقة",
                "name_en": "Recovery Room Nursing",
                "location_id": "48",
                "main_section_id": "2",
            },
            {
                "id": "182",
                "name_ar": "تمريض قسم التخدير",
                "name_en": "Anesthesia Nursing",
                "location_id": "48",
                "main_section_id": "2",
            },
            {
                "id": "183",
                "name_ar": "تمريض قسم العمليات",
                "name_en": "OR Nursing",
                "location_id": "48",
                "main_section_id": "2",
            },
            {
                "id": "163",
                "name_ar": "تمريض قسم المناظير",
                "name_en": "Endoscopy Nursing",
                "location_id": "48",
                "main_section_id": "2",
            },
            {
                "id": "178",
                "name_ar": "تمريض قسم عمليات الولادة",
                "name_en": "Delivery OR Nursing",
                "location_id": "48",
                "main_section_id": "2",
            },
            {
                "id": "161",
                "name_ar": "تمريض وحدة طويلي الإقامة",
                "name_en": "Long-term Care Nursing",
                "location_id": "48",
                "main_section_id": "2",
            },
            {
                "id": "159",
                "name_ar": "تمريض وحدة العناية المتوسطة",
                "name_en": "Intermediate Care Nursing",
                "location_id": "48",
                "main_section_id": "2",
            },
            {
                "id": "160",
                "name_ar": "تمريض وحدة العناية المتوسطة - أطفال",
                "name_en": "Pediatric Intermediate Care Nursing",
                "location_id": "48",
                "main_section_id": "2",
            },
            {
                "id": "156",
                "name_ar": "تمريض وحدة العناية المركزة",
                "name_en": "ICU Nursing",
                "location_id": "48",
                "main_section_id": "2",
            },
            {
                "id": "157",
                "name_ar": "تمريض وحدة العناية المركزة - أطفال",
                "name_en": "PICU Nursing",
                "location_id": "48",
                "main_section_id": "2",
            },
            {
                "id": "158",
                "name_ar": "تمريض وحدة العناية لحديثي الولادة",
                "name_en": "NICU Nursing",
                "location_id": "48",
                "main_section_id": "2",
            },
            {
                "id": "162",
                "name_ar": "تمريض وحدة غسيل الكلى",
                "name_en": "Hemodialysis Nursing",
                "location_id": "48",
                "main_section_id": "2",
            },
            {
                "id": "207",
                "name_ar": "قسم الصيانة",
                "name_en": "Maintenance Department",
                "location_id": "48",
                "main_section_id": "3",
            },
            {
                "id": "164",
                "name_ar": "قسم المطبخ",
                "name_en": "Kitchen Department",
                "location_id": "48",
                "main_section_id": "3",
            },
            {
                "id": "165",
                "name_ar": "قسم النظافة",
                "name_en": "Housekeeping Department",
                "location_id": "48",
                "main_section_id": "3",
            },
            {
                "id": "172",
                "name_ar": "إدارة التنويم",
                "name_en": "Admission Management",
                "location_id": "48",
                "main_section_id": "4",
            },
            {
                "id": "226",
                "name_ar": "التقارير و الشهادات الطبية",
                "name_en": "Medical Reports & Certificates",
                "location_id": "48",
                "main_section_id": "4",
            },
            {
                "id": "174",
                "name_ar": "المدير المناوب",
                "name_en": "Duty Manager",
                "location_id": "48",
                "main_section_id": "4",
            },
            {
                "id": "229",
                "name_ar": "تشخيص الحالة الصحية و شرح الخيارات العلاجية",
                "name_en": "Diagnosis & Treatment Options Explanation",
                "location_id": "48",
                "main_section_id": "4",
            },
            {
                "id": "105",
                "name_ar": "قسم الأمن",
                "name_en": "Security Department",
                "location_id": "48",
                "main_section_id": "4",
            },
            {
                "id": "171",
                "name_ar": "قسم الخدمة الإجتماعية",
                "name_en": "Social Services Department",
                "location_id": "48",
                "main_section_id": "4",
            },
            {
                "id": "168",
                "name_ar": "قسم السنترال",
                "name_en": "Operator / PBX",
                "location_id": "48",
                "main_section_id": "4",
            },
            {
                "id": "169",
                "name_ar": "قسم المالية",
                "name_en": "Finance Department",
                "location_id": "48",
                "main_section_id": "4",
            },
            {
                "id": "167",
                "name_ar": "قسم المواعيد",
                "name_en": "Appointments Department",
                "location_id": "48",
                "main_section_id": "4",
            },
            {
                "id": "173",
                "name_ar": "قسم الموافقات الطبية",
                "name_en": "Medical Approvals Department",
                "location_id": "48",
                "main_section_id": "4",
            },
            {
                "id": "170",
                "name_ar": "قسم علاقات المرضى",
                "name_en": "Patient Relations Department",
                "location_id": "48",
                "main_section_id": "4",
            },
            {
                "id": "230",
                "name_ar": "متعلقات مالية",
                "name_en": "Financial Matters",
                "location_id": "48",
                "main_section_id": "4",
            },
            {
                "id": "184",
                "name_ar": "منسقة مراجعي مستشفى قوى الأمن",
                "name_en": "SFH Patient Staff",
                "location_id": "48",
                "main_section_id": "4",
            },
            # { "id": "213", "name_ar": "قسم تقنية المعلومات", "name_en": "IT Department", "location_id": "48", "main_section_id": "5" },
            # /* --- LOCATION: العيادات الخارجية (49) - Outpatient --- */
            {
                "id": "197",
                "name_ar": "الأشعة التداخلية للأوعية الدموية و الثدي",
                "name_en": "Vascular & Breast Interventional Radiology",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "227",
                "name_ar": "التقارير الطبية",
                "name_en": "Medical Reports",
                "location_id": "49",
                "main_section_id": "1",
            },
            {"id": "211", "name_ar": "بنك الدم", "name_en": "Blood Bank", "location_id": "49", "main_section_id": "1"},
            {
                "id": "29",
                "name_ar": "عيادات أمراض دم (أطفال)",
                "name_en": "Pediatric Hematology Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "179",
                "name_ar": "عيادات الأسنان",
                "name_en": "Dental Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "25",
                "name_ar": "عيادات الأطفال",
                "name_en": "Pediatric Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "199",
                "name_ar": "عيادات الأمراض المعدية",
                "name_en": "Infectious Diseases Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "196",
                "name_ar": "عيادات الأنف والأذن والحنجرة (أطفال)",
                "name_en": "Pediatric ENT Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "1",
                "name_ar": "عيادات الباطنية",
                "name_en": "Internal Medicine Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "109",
                "name_ar": "عيادات التخدير",
                "name_en": "Anesthesia Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "32",
                "name_ar": "عيادات الجراحة التجميلية",
                "name_en": "Plastic Surgery Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "16",
                "name_ar": "عيادات الجراحة العامة",
                "name_en": "General Surgery Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "190",
                "name_ar": "عيادات الجراحة العامة (أطفال)",
                "name_en": "Pediatric General Surgery Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "10",
                "name_ar": "عيادات الجلدية",
                "name_en": "Dermatology Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "9",
                "name_ar": "عيادات الجهاز الهضمي والمناظير",
                "name_en": "Gastroenterology & Endoscopy Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "28",
                "name_ar": "عيادات الجهاز الهمضي والمناظير (أطفال)",
                "name_en": "Pediatric Gastroenterology Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "200",
                "name_ar": "عيادات الروماتيزم",
                "name_en": "Rheumatology Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "5",
                "name_ar": "عيادات الصدرية",
                "name_en": "Pulmonary Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "191",
                "name_ar": "عيادات الصدرية (أطفال)",
                "name_en": "Pediatric Pulmonary Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "11",
                "name_ar": "عيادات الطب النفسي",
                "name_en": "Psychiatry Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "198",
                "name_ar": "عيادات العقم والإنجاب",
                "name_en": "Infertility & Reproductive Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "12",
                "name_ar": "عيادات العيون",
                "name_en": "Ophthalmology Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "13",
                "name_ar": "عيادات الغدد الصماء",
                "name_en": "Endocrinology Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "27",
                "name_ar": "عيادات الغدد الصماء والسكري (أطفال)",
                "name_en": "Pediatric Endo & Diabetes Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "2",
                "name_ar": "عيادات القلب",
                "name_en": "Cardiology Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "6",
                "name_ar": "عيادات الكلى",
                "name_en": "Nephrology Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "189",
                "name_ar": "عيادات الكلى (أطفال)",
                "name_en": "Pediatric Nephrology Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "14",
                "name_ar": "عيادات المخ والأعصاب",
                "name_en": "Neurology Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "26",
                "name_ar": "عيادات المخ والأعصاب (أطفال)",
                "name_en": "Pediatric Neurology Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "31",
                "name_ar": "عيادات النساء والولادة",
                "name_en": "OB/GYN Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "188",
                "name_ar": "عيادات امراض الدم",
                "name_en": "Hematology Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "20",
                "name_ar": "عيادات جراحة الأنف وأذن وحنجرة",
                "name_en": "ENT Surgery Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "24",
                "name_ar": "عيادات جراحة الأورام و الغدد الصماء",
                "name_en": "Oncology & Endo Surgery Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "15",
                "name_ar": "عيادات جراحة الأوعية الدموية",
                "name_en": "Vascular Surgery Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "209",
                "name_ar": "عيادات جراحة الختان",
                "name_en": "Circumcision Surgery Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "17",
                "name_ar": "عيادات جراحة العظام",
                "name_en": "Orthopedic Surgery Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "194",
                "name_ar": "عيادات جراحة العمود الفقري",
                "name_en": "Spine Surgery Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "0",
                "name_ar": "عيادات جراحة العيون",
                "name_en": "Eye Surgery Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "23",
                "name_ar": "عيادات جراحة القفص الصدري",
                "name_en": "Thoracic Surgery Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "21",
                "name_ar": "عيادات جراحة القلب",
                "name_en": "Cardiac Surgery Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "22",
                "name_ar": "عيادات جراحة المخ والأعصاب",
                "name_en": "Neurosurgery Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "195",
                "name_ar": "عيادات جراحة المخ والأعصاب (أطفال)",
                "name_en": "Pediatric Neurosurgery Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "18",
                "name_ar": "عيادات جراحة المسالك البولية",
                "name_en": "Urology Surgery Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "192",
                "name_ar": "عيادات جراحة المسالك البولية (أطفال)",
                "name_en": "Pediatric Urology Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "193",
                "name_ar": "عيادات جراحة سمنة",
                "name_en": "Bariatric Surgery Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "202",
                "name_ar": "عيادات جراحة عظام (أطفال)",
                "name_en": "Pediatric Ortho Surgery Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "30",
                "name_ar": "عيادات قلب (أطفال)",
                "name_en": "Pediatric Cardiology Clinics",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "58",
                "name_ar": "فني أشعة القلب الارتدادية",
                "name_en": "Echocardiogram Technician",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "60",
                "name_ar": "فني اختبار التنفس",
                "name_en": "PFT Technician",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "201",
                "name_ar": "فني اختبار السمع",
                "name_en": "Audiology Technician",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "62",
                "name_ar": "فني التجبير",
                "name_en": "Plaster/Casting Technician",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "59",
                "name_ar": "فني تخطيط القلب",
                "name_en": "ECG Technician",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "56",
                "name_ar": "فني تخطيط المخ والأعصاب",
                "name_en": "EEG Technician",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "57",
                "name_ar": "فني دراسة الجهد القلب",
                "name_en": "Stress Test Technician",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "61",
                "name_ar": "فني فحص النظر",
                "name_en": "Optometry Technician",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "186",
                "name_ar": "قسم الأشعة",
                "name_en": "Radiology Department",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "206",
                "name_ar": "قسم التغذية",
                "name_en": "Nutrition Department",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "54",
                "name_ar": "قسم الصيدلية",
                "name_en": "Pharmacy Department",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "205",
                "name_ar": "قسم الصيدلية - السويدي",
                "name_en": "Pharmacy - Al Suwaidi",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "187",
                "name_ar": "قسم العلاج الطبيعي",
                "name_en": "Physiotherapy Department",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "180",
                "name_ar": "قسم المختبر",
                "name_en": "Laboratory Department",
                "location_id": "49",
                "main_section_id": "1",
            },
            {
                "id": "185",
                "name_ar": "تمريض التخدير",
                "name_en": "Anesthesia Nursing",
                "location_id": "49",
                "main_section_id": "2",
            },
            {
                "id": "3",
                "name_ar": "تمريض العيادات الخارجية",
                "name_en": "Outpatient Nursing",
                "location_id": "49",
                "main_section_id": "2",
            },
            {
                "id": "79",
                "name_ar": "تمريض غرفة التطعيمات",
                "name_en": "Vaccination Room Nursing",
                "location_id": "49",
                "main_section_id": "2",
            },
            {
                "id": "110",
                "name_ar": "تمريض غرفة تقديم الأدوية الوريدية (20)",
                "name_en": "IV Room Nursing (20)",
                "location_id": "49",
                "main_section_id": "2",
            },
            {
                "id": "78",
                "name_ar": "تمريض مكتب التنويم",
                "name_en": "Admission Office Nursing",
                "location_id": "49",
                "main_section_id": "2",
            },
            {
                "id": "225",
                "name_ar": "اخصائي تخطيط المخ والاعصاب",
                "name_en": "EEG Specialist",
                "location_id": "49",
                "main_section_id": "3",
            },
            {
                "id": "84",
                "name_ar": "قسم النظافة",
                "name_en": "Housekeeping Department",
                "location_id": "49",
                "main_section_id": "3",
            },
            {
                "id": "223",
                "name_ar": "إدارة العيادات الخارجيه",
                "name_en": "Outpatient Management",
                "location_id": "49",
                "main_section_id": "4",
            },
            {
                "id": "93",
                "name_ar": "استقبال الأشعة",
                "name_en": "Radiology Reception",
                "location_id": "49",
                "main_section_id": "4",
            },
            {
                "id": "94",
                "name_ar": "استقبال العلاج الطبيعي",
                "name_en": "Physiotherapy Reception",
                "location_id": "49",
                "main_section_id": "4",
            },
            {
                "id": "113",
                "name_ar": "استقبال العيادات الخارجية",
                "name_en": "Outpatient Reception",
                "location_id": "49",
                "main_section_id": "4",
            },
            {
                "id": "92",
                "name_ar": "استقبال المختبر",
                "name_en": "Laboratory Reception",
                "location_id": "49",
                "main_section_id": "4",
            },
            {"id": "215", "name_ar": "التنسيق", "name_en": "Coordination", "location_id": "49", "main_section_id": "4"},
            {
                "id": "222",
                "name_ar": "الدعم الفني",
                "name_en": "Technical Support",
                "location_id": "49",
                "main_section_id": "4",
            },
            {"id": "219", "name_ar": "المختبر", "name_en": "Laboratory", "location_id": "49", "main_section_id": "4"},
            {
                "id": "111",
                "name_ar": "قسم الأمن",
                "name_en": "Security Department",
                "location_id": "49",
                "main_section_id": "4",
            },
            {
                "id": "203",
                "name_ar": "قسم الإدارة",
                "name_en": "Administration Department",
                "location_id": "49",
                "main_section_id": "4",
            },
            {
                "id": "102",
                "name_ar": "قسم التقارير الطبية",
                "name_en": "Medical Reports Department",
                "location_id": "49",
                "main_section_id": "4",
            },
            {
                "id": "224",
                "name_ar": "قسم الدعم الفني",
                "name_en": "IT Helpdesk",
                "location_id": "49",
                "main_section_id": "4",
            },
            {
                "id": "103",
                "name_ar": "قسم السكرتارية الطبية",
                "name_en": "Medical Secretarial Department",
                "location_id": "49",
                "main_section_id": "4",
            },
            {
                "id": "89",
                "name_ar": "قسم السنترال",
                "name_en": "Operator / PBX",
                "location_id": "49",
                "main_section_id": "4",
            },
            {
                "id": "90",
                "name_ar": "قسم المالية",
                "name_en": "Finance Department",
                "location_id": "49",
                "main_section_id": "4",
            },
            {
                "id": "88",
                "name_ar": "قسم المواعيد",
                "name_en": "Appointments Department",
                "location_id": "49",
                "main_section_id": "4",
            },
            {
                "id": "104",
                "name_ar": "قسم الموافقات الطبية",
                "name_en": "Medical Approvals Department",
                "location_id": "49",
                "main_section_id": "4",
            },
            {
                "id": "97",
                "name_ar": "قسم علاقات المرضى",
                "name_en": "Patient Relations Department",
                "location_id": "49",
                "main_section_id": "4",
            },
            {
                "id": "99",
                "name_ar": "مكتب التنويم",
                "name_en": "Admission Office",
                "location_id": "49",
                "main_section_id": "4",
            },
            # { "id": "213", "name_ar": "قسم تقنية المعلومات", "name_en": "IT Department", "location_id": "49", "main_section_id": "5" },
            # /* --- LOCATION: الطوارئ (82) - Emergency --- */
            {
                "id": "34",
                "name_ar": "أطباء الطوارئ",
                "name_en": "ER Doctors",
                "location_id": "82",
                "main_section_id": "1",
            },
            {"id": "49", "name_ar": "الأشعة", "name_en": "Radiology", "location_id": "82", "main_section_id": "1"},
            {
                "id": "53",
                "name_ar": "صيدلية الطوارئ",
                "name_en": "ER Pharmacy",
                "location_id": "82",
                "main_section_id": "1",
            },
            {
                "id": "77",
                "name_ar": "تمريض الطوارئ",
                "name_en": "ER Nursing",
                "location_id": "82",
                "main_section_id": "2",
            },
            {
                "id": "149",
                "name_ar": "قسم النظافة",
                "name_en": "Housekeeping Department",
                "location_id": "82",
                "main_section_id": "3",
            },
            {
                "id": "96",
                "name_ar": "استقبال الطوارئ",
                "name_en": "ER Reception",
                "location_id": "82",
                "main_section_id": "4",
            },
            {
                "id": "228",
                "name_ar": "التقارير الطبية",
                "name_en": "Medical Reports",
                "location_id": "82",
                "main_section_id": "4",
            },
            {
                "id": "100",
                "name_ar": "المدير المناوب",
                "name_en": "Duty Manager",
                "location_id": "82",
                "main_section_id": "4",
            },
            {
                "id": "98",
                "name_ar": "الموافقات الطبية",
                "name_en": "Medical Approvals",
                "location_id": "82",
                "main_section_id": "4",
            },
            {
                "id": "220",
                "name_ar": "تنسيق الطوارئ",
                "name_en": "ER Coordination",
                "location_id": "82",
                "main_section_id": "4",
            },
            {
                "id": "95",
                "name_ar": "قسم الأمن",
                "name_en": "Security Department",
                "location_id": "82",
                "main_section_id": "4",
            },
            {
                "id": "210",
                "name_ar": "مكتب التنويم",
                "name_en": "Admission Office",
                "location_id": "82",
                "main_section_id": "4",
            },
            # { "id": "213", "name_ar": "قسم تقنية المعلومات", "name_en": "IT Department", "location_id": "82", "main_section_id": "5" },
            # /* --- LOCATION: اخرى (110) - Others --- */
            {
                "id": "213",
                "name_ar": "قسم تقنية المعلومات",
                "name_en": "IT Department",
                "location_id": "110",
                "main_section_id": "5",
            },
        ]

        # Create Locations
        for loc in locations_data:
            LegacyLocation.objects.update_or_create(
                id=loc["id"], defaults={"name_ar": loc["name_ar"], "name_en": loc["name_en"]}
            )

        # Create Main Sections
        for sec in main_sections_data:
            LegacyMainSection.objects.update_or_create(
                id=sec["id"], defaults={"name_ar": sec["name_ar"], "name_en": sec["name_en"]}
            )

        subsections_to_create = []

        # Clear existing data to prevent old ID conflicts (skip if referenced)
        try:
            LegacySubSection.objects.all().delete()
        except Exception:
            self.stdout.write(self.style.WARNING("Skipping SubSection deletion - some are referenced by complaints"))

        for item in subsections_data:
            subsections_to_create.append(
                LegacySubSection(
                    internal_id=int(item["id"]),
                    name_en=item["name_en"],
                    name_ar=item["name_ar"],
                    location_id=int(item["location_id"]),
                    main_section_id=int(item["main_section_id"]),
                )
            )

        # Use bulk_create for speed
        LegacySubSection.objects.bulk_create(subsections_to_create, ignore_conflicts=True)
        # Bulk Create SubSections
        # objs = [
        #     SubSection(
        #         id=item['id'],
        #         name=item['name'],
        #         location_id=item['location_id'],
        #         main_section_id=item['main_section_id']
        #     ) for item in subsections_data
        # ]

        # # Using ignore_conflicts=True in case of duplicate IDs like '213'
        # SubSection.objects.bulk_create(objs, ignore_conflicts=True)

        self.stdout.write(self.style.SUCCESS("Database populated successfully!"))
