from django.core.management.base import BaseCommand
from apps.organizations.models import Department

HR_NAME_MAP = {
    "Anesthesia And Or": "Anesthesia Department",
    "Critical Care": "Critical Care Department",
    "Dermatology": "Dermatology Department",
    "Facility Management & Maintenance": "Facility Management & Maintenance Department",
    "Infection Control": "Infection Control Department",
    "Information Technology": "Information Technology Department",
    "Internal Medicine": "Internal Medicine Department",
    "Laboratory And Blood Bank": "Laboratory Department",
    "Medical Ancillary Services": "Medical Ancillary Services Department",
    "Obstetrics And Gynecology": "Obstetrics & Gynecology Department",
    "Oncology": "ONCOLOGY Department",
    "Ophthalmology": "Ophthalmology Department",
    "Radiology": "Radiology Department",
    "Dentistry": "Dental Department",
    "Surgeries": "Surgery Department",
    "Pediatrics": "Pediatric Department",
    "Emergency Department": "Emergency Medicine Department",
    "Accident And Emergency": "Emergency Medicine Department",
    "Human Resource": "HR Department",
    "Support Services": "Support Services Department",
    "Finance Department": "Financial Collection & Claims Department",
    "Corporate Administration": "Executive Administration",
    "Senior Management Offices": "Executive Administration",
    "Patient Relations & Patient Experience Department": "Patient Experience Department",
    "Porter Department": "Security Department",
    "Transportation Department": "Security Department",
    "Corporate Communication Department": "Patient Experience Department",
    "Marketing Department": "Patient Experience Department",
    "Supply Chain": "Facility Management & Maintenance Department",
    "Pharmacy Warehouse Alaziziyah": "Pharmacy Department",
    "Family Medicine": "Outpatient Department",
    "Business Development": "Executive Administration",
    "Continuous Medical Education Managment": "Medical Administration",
    "Academic Education And Training Affairs": "Medical Administration",
    "Innovation Communication Management": "Medical Administration",
    "Transformation And Change Management": "Medical Administration",
    "Talent Acquisition Department": "HR Department",
    "Internal Audit": "Financial Collection & Claims Department",
    "Legal Affairs Department": "Executive Administration",
    "Cybersecurity Management": "Information Technology Department",
    "Rehabilitation Center": "Outpatient Department",
}


class Command(BaseCommand):
    help = "Set hr_name on Department records from HR system name mapping"

    def handle(self, *args, **options):
        updated = 0
        for hr_name, name_en in HR_NAME_MAP.items():
            depts = Department.objects.filter(
                name_en__iexact=name_en, status="active", hr_name=""
            )
            for dept in depts:
                dept.hr_name = hr_name
                dept.save(update_fields=["hr_name"])
                updated += 1
                self.stdout.write(f"  {dept.code}: hr_name={repr(hr_name)}")

        self.stdout.write(self.style.SUCCESS(f"\nUpdated {updated} departments"))
