"""
PX Sources serializers
"""
from rest_framework import serializers

from .models import PXSource, SourceUser


class PXSourceSerializer(serializers.ModelSerializer):
    """Serializer for PXSource model"""
    class Meta:
        model = PXSource
        fields = [
            'id', 'code', 'name_en', 'name_ar',
            'description', 'source_type',
            'contact_email', 'contact_phone',
            'is_active', 'metadata',
            'total_complaints', 'total_inquiries',
            'created_at', 'updated_at'
        ]
        read_only_fields = ['id', 'created_at', 'updated_at', 'total_complaints', 'total_inquiries']


class PXSourceListSerializer(serializers.ModelSerializer):
    """Simplified serializer for list views"""
    class Meta:
        model = PXSource
        fields = [
            'id', 'code', 'name_en', 'name_ar',
            'source_type', 'is_active',
            'total_complaints', 'total_inquiries'
        ]


class PXSourceDetailSerializer(PXSourceSerializer):
    """Detailed serializer including usage statistics"""
    usage_count = serializers.SerializerMethodField()
    recent_usage = serializers.SerializerMethodField()

    class Meta(PXSourceSerializer.Meta):
        fields = PXSourceSerializer.Meta.fields + ['usage_count', 'recent_usage']

    def get_usage_count(self, obj):
        """Get total usage count for this source"""
        return obj.usage_records.count()

    def get_recent_usage(self, obj):
        """Get recent usage stats (last 30 days)"""
        return obj.get_usage_stats(days=30)


class SourceUserSerializer(serializers.ModelSerializer):
    """Serializer for SourceUser model"""
    user_email = serializers.EmailField(source='user.email', read_only=True)
    user_full_name = serializers.CharField(source='user.get_full_name', read_only=True)
    source_name = serializers.CharField(source='source.name_en', read_only=True)
    source_name_ar = serializers.CharField(source='source.name_ar', read_only=True)
    source_code = serializers.CharField(source='source.code', read_only=True)

    class Meta:
        model = SourceUser
        fields = [
            'id',
            'user',
            'user_email',
            'user_full_name',
            'source',
            'source_code',
            'source_name',
            'source_name_ar',
            'is_active',
            'can_create_complaints',
            'can_create_inquiries',
            'created_at',
            'updated_at'
        ]
        read_only_fields = ['id', 'created_at', 'updated_at']


class SourceUserListSerializer(serializers.ModelSerializer):
    """Simplified serializer for source user list views"""
    user_email = serializers.EmailField(source='user.email', read_only=True)
    user_full_name = serializers.CharField(source='user.get_full_name', read_only=True)
    source_name = serializers.CharField(source='source.name_en', read_only=True)
    source_code = serializers.CharField(source='source.code', read_only=True)

    class Meta:
        model = SourceUser
        fields = [
            'id',
            'user_email',
            'user_full_name',
            'source_code',
            'source_name',
            'is_active',
            'can_create_complaints',
            'can_create_inquiries'
        ]


class SourceUsageSerializer(serializers.ModelSerializer):
    """Serializer for SourceUsage model"""
    source_name = serializers.CharField(source='source.name_en', read_only=True)
    source_code = serializers.CharField(source='source.code', read_only=True)
    content_type_name = serializers.CharField(source='content_type.model', read_only=True)
    hospital_name = serializers.CharField(source='hospital.name', read_only=True)
    user_email = serializers.EmailField(source='user.email', read_only=True)

    class Meta:
        model = PXSource
        fields = [
            'id', 'source', 'source_code', 'source_name',
            'content_type', 'content_type_name', 'object_id',
            'hospital', 'hospital_name', 'user', 'user_email', 'created_at'
        ]
        read_only_fields = ['id', 'created_at']


class PXSourceChoiceSerializer(serializers.Serializer):
    """Simple serializer for dropdown choices"""
    id = serializers.UUIDField()
    code = serializers.CharField()
    name = serializers.SerializerMethodField()

    def get_name(self, obj):
        """Get localized name based on request language"""
        request = self.context.get('request')
        if request:
            language = getattr(request, 'LANGUAGE_CODE', 'en')
            return obj.get_localized_name(language)
        return obj.name_en