""" Organizations serializers """ from rest_framework import serializers from .models import Department, Employee, Hospital, Patient, Physician class HospitalSerializer(serializers.ModelSerializer): """Hospital serializer""" class Meta: model = Hospital fields = [ 'id', 'name', 'name_ar', 'code', 'address', 'city', 'phone', 'email', 'status', 'license_number', 'capacity', 'created_at', 'updated_at' ] read_only_fields = ['id', 'created_at', 'updated_at'] class DepartmentSerializer(serializers.ModelSerializer): """Department serializer""" hospital_name = serializers.CharField(source='hospital.name', read_only=True) parent_name = serializers.CharField(source='parent.name', read_only=True) manager_name = serializers.SerializerMethodField() class Meta: model = Department fields = [ 'id', 'hospital', 'hospital_name', 'name', 'name_ar', 'code', 'parent', 'parent_name', 'manager', 'manager_name', 'phone', 'email', 'location', 'status', 'created_at', 'updated_at' ] read_only_fields = ['id', 'created_at', 'updated_at'] def get_manager_name(self, obj): """Get manager full name""" if obj.manager: return obj.manager.get_full_name() return None class PhysicianSerializer(serializers.ModelSerializer): """Physician serializer""" hospital_name = serializers.CharField(source='hospital.name', read_only=True) department_name = serializers.CharField(source='department.name', read_only=True) full_name = serializers.CharField(source='get_full_name', read_only=True) class Meta: model = Physician fields = [ 'id', 'user', 'first_name', 'last_name', 'first_name_ar', 'last_name_ar', 'full_name', 'license_number', 'specialization', 'hospital', 'hospital_name', 'department', 'department_name', 'phone', 'email', 'status', 'created_at', 'updated_at' ] read_only_fields = ['id', 'created_at', 'updated_at'] class EmployeeSerializer(serializers.ModelSerializer): """Employee serializer""" user_email = serializers.EmailField(source='user.email', read_only=True) user_name = serializers.SerializerMethodField() hospital_name = serializers.CharField(source='hospital.name', read_only=True) department_name = serializers.CharField(source='department.name', read_only=True) class Meta: model = Employee fields = [ 'id', 'user', 'user_email', 'user_name', 'employee_id', 'job_title', 'hospital', 'hospital_name', 'department', 'department_name', 'hire_date', 'status', 'created_at', 'updated_at' ] read_only_fields = ['id', 'created_at', 'updated_at'] def get_user_name(self, obj): """Get user full name""" return obj.user.get_full_name() class PatientSerializer(serializers.ModelSerializer): """Patient serializer""" primary_hospital_name = serializers.CharField(source='primary_hospital.name', read_only=True) full_name = serializers.CharField(source='get_full_name', read_only=True) age = serializers.SerializerMethodField() class Meta: model = Patient fields = [ 'id', 'mrn', 'national_id', 'first_name', 'last_name', 'first_name_ar', 'last_name_ar', 'full_name', 'date_of_birth', 'age', 'gender', 'phone', 'email', 'address', 'city', 'primary_hospital', 'primary_hospital_name', 'status', 'created_at', 'updated_at' ] read_only_fields = ['id', 'created_at', 'updated_at'] def get_age(self, obj): """Calculate patient age""" if obj.date_of_birth: from datetime import date today = date.today() return today.year - obj.date_of_birth.year - ( (today.month, today.day) < (obj.date_of_birth.month, obj.date_of_birth.day) ) return None class PatientListSerializer(serializers.ModelSerializer): """Simplified patient serializer for list views""" full_name = serializers.CharField(source='get_full_name', read_only=True) primary_hospital_name = serializers.CharField(source='primary_hospital.name', read_only=True) class Meta: model = Patient fields = [ 'id', 'mrn', 'full_name', 'phone', 'email', 'primary_hospital_name', 'status' ]