"""
Organizations serializers
"""
from rest_framework import serializers

from .models import Department, Hospital, Organization, Patient, Staff


class OrganizationSerializer(serializers.ModelSerializer):
    """Organization serializer"""
    hospitals_count = serializers.SerializerMethodField()

    class Meta:
        model = Organization
        fields = [
            'id', 'name', 'name_ar', 'code', 'address', 'city',
            'phone', 'email', 'website', 'status', 'license_number',
            'logo', 'hospitals_count', 'created_at', 'updated_at'
        ]
        read_only_fields = ['id', 'created_at', 'updated_at']

    def get_hospitals_count(self, obj):
        """Get count of hospitals in this organization"""
        return obj.hospitals.count()


class HospitalSerializer(serializers.ModelSerializer):
    """Hospital serializer"""
    organization_name = serializers.CharField(source='organization.name', read_only=True)
    departments_count = serializers.SerializerMethodField()

    class Meta:
        model = Hospital
        fields = [
            'id', 'organization', 'organization_name', 'name', 'name_ar', 'code',
            'address', 'city', 'phone', 'email', 'status',
            'license_number', 'capacity', 'departments_count',
            'created_at', 'updated_at'
        ]
        read_only_fields = ['id', 'created_at', 'updated_at']

    def get_departments_count(self, obj):
        """Get count of departments in this hospital"""
        return obj.departments.count()


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 StaffSerializer(serializers.ModelSerializer):
    """Staff 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)
    user_email = serializers.EmailField(source='user.email', read_only=True, allow_null=True)

    class Meta:
        model = Staff
        fields = [
            'id', 'user', 'first_name', 'last_name', 'first_name_ar', 'last_name_ar',
            'full_name', 'staff_type', 'job_title',
            'license_number', 'specialization', 'employee_id',
            'hospital', 'hospital_name', 'department', 'department_name',
            'user_email', 'status',
            'created_at', 'updated_at'
        ]
        read_only_fields = ['id', 'created_at', 'updated_at']


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'
        ]
