"""
Physicians serializers
"""
from rest_framework import serializers

from apps.organizations.models import Staff

from .models import PhysicianMonthlyRating


class PhysicianSerializer(serializers.ModelSerializer):
    """Physician serializer"""
    full_name = serializers.CharField(source='get_full_name', read_only=True)
    hospital_name = serializers.CharField(source='hospital.name', read_only=True)
    department_name = serializers.CharField(source='department.name', read_only=True)

    class Meta:
        model = Staff
        fields = [
            'id', '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 PhysicianMonthlyRatingSerializer(serializers.ModelSerializer):
    """Physician monthly rating serializer"""
    staff_name = serializers.CharField(source='staff.get_full_name', read_only=True)
    staff_license = serializers.CharField(source='staff.license_number', read_only=True)
    hospital_name = serializers.CharField(source='staff.hospital.name', read_only=True)
    department_name = serializers.CharField(source='staff.department.name', read_only=True)
    month_name = serializers.SerializerMethodField()

    class Meta:
        model = PhysicianMonthlyRating
        fields = [
            'id', 'staff', 'staff_name', 'staff_license',
            'hospital_name', 'department_name',
            'year', 'month', 'month_name',
            'average_rating', 'total_surveys',
            'positive_count', 'neutral_count', 'negative_count',
            'md_consult_rating',
            'hospital_rank', 'department_rank',
            'metadata',
            'created_at', 'updated_at'
        ]
        read_only_fields = ['id', 'created_at', 'updated_at']

    def get_month_name(self, obj):
        """Get month name"""
        months = [
            'January', 'February', 'March', 'April', 'May', 'June',
            'July', 'August', 'September', 'October', 'November', 'December'
        ]
        return months[obj.month - 1] if 1 <= obj.month <= 12 else ''


class PhysicianLeaderboardSerializer(serializers.Serializer):
    """Physician leaderboard serializer"""
    physician_id = serializers.UUIDField()
    physician_name = serializers.CharField()
    physician_license = serializers.CharField()
    specialization = serializers.CharField()
    department_name = serializers.CharField()
    average_rating = serializers.DecimalField(max_digits=3, decimal_places=2)
    total_surveys = serializers.IntegerField()
    rank = serializers.IntegerField()
    trend = serializers.CharField(required=False)  # 'up', 'down', 'stable'


class PhysicianPerformanceSerializer(serializers.Serializer):
    """Physician performance summary serializer"""
    physician = PhysicianSerializer()
    current_month_rating = PhysicianMonthlyRatingSerializer(required=False, allow_null=True)
    previous_month_rating = PhysicianMonthlyRatingSerializer(required=False, allow_null=True)
    year_to_date_average = serializers.DecimalField(max_digits=3, decimal_places=2, required=False, allow_null=True)
    total_surveys_ytd = serializers.IntegerField(required=False)
    best_month = PhysicianMonthlyRatingSerializer(required=False, allow_null=True)
    worst_month = PhysicianMonthlyRatingSerializer(required=False, allow_null=True)
    trend = serializers.CharField(required=False)  # 'improving', 'declining', 'stable'
