#!/usr/bin/env python
import os
import django

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.base')
django.setup()

from apps.organizations.models import Department

depts = Department.objects.all()
print(f'Total departments: {depts.count()}')
print('\nDepartments by hospital:')

for hosp in sorted(set(d.hospital for d in depts), key=lambda h: h.name):
    hosp_depts = Department.objects.filter(hospital=hosp).order_by('code')
    print(f'\n{hosp.name} ({hosp.code}):')
    for d in hosp_depts:
        manager_name = f' (Manager: {d.manager.get_full_name()})' if d.manager else ' (No manager)'
        print(f'  - {d.code}: {d.name} / {d.name_ar}{manager_name}')
