"""
PX Action Center serializers
"""
from rest_framework import serializers

from .models import PXAction, PXActionAttachment, PXActionLog, PXActionSLAConfig, RoutingRule


class PXActionLogSerializer(serializers.ModelSerializer):
    """PX Action log serializer"""
    created_by_name = serializers.SerializerMethodField()
    
    class Meta:
        model = PXActionLog
        fields = [
            'id', 'action', 'log_type', 'message',
            'created_by', 'created_by_name',
            'old_status', 'new_status', 'metadata',
            'created_at', 'updated_at'
        ]
        read_only_fields = ['id', 'created_at', 'updated_at']
    
    def get_created_by_name(self, obj):
        """Get creator name"""
        if obj.created_by:
            return obj.created_by.get_full_name()
        return None


class PXActionAttachmentSerializer(serializers.ModelSerializer):
    """PX Action attachment serializer"""
    uploaded_by_name = serializers.SerializerMethodField()
    
    class Meta:
        model = PXActionAttachment
        fields = [
            'id', 'action', 'file', 'filename', 'file_type', 'file_size',
            'uploaded_by', 'uploaded_by_name', 'description', 'is_evidence',
            'created_at', 'updated_at'
        ]
        read_only_fields = ['id', 'file_size', 'created_at', 'updated_at']
    
    def get_uploaded_by_name(self, obj):
        """Get uploader name"""
        if obj.uploaded_by:
            return obj.uploaded_by.get_full_name()
        return None


class PXActionSerializer(serializers.ModelSerializer):
    """PX Action serializer"""
    hospital_name = serializers.CharField(source='hospital.name', read_only=True)
    department_name = serializers.CharField(source='department.name', read_only=True)
    assigned_to_name = serializers.SerializerMethodField()
    approved_by_name = serializers.SerializerMethodField()
    closed_by_name = serializers.SerializerMethodField()
    logs = PXActionLogSerializer(many=True, read_only=True)
    attachments = PXActionAttachmentSerializer(many=True, read_only=True)
    sla_status = serializers.SerializerMethodField()
    
    class Meta:
        model = PXAction
        fields = [
            'id', 'source_type', 'content_type', 'object_id',
            'title', 'description', 'category',
            'hospital', 'hospital_name', 'department', 'department_name',
            'priority', 'severity', 'status',
            'assigned_to', 'assigned_to_name', 'assigned_at',
            'due_at', 'is_overdue', 'sla_status',
            'reminder_sent_at', 'escalated_at', 'escalation_level',
            'requires_approval', 'approved_by', 'approved_by_name', 'approved_at',
            'closed_at', 'closed_by', 'closed_by_name',
            'action_plan', 'outcome',
            'logs', 'attachments', 'metadata',
            'created_at', 'updated_at'
        ]
        read_only_fields = [
            'id', 'assigned_at', 'is_overdue',
            'reminder_sent_at', 'escalated_at',
            'approved_at', 'closed_at',
            'created_at', 'updated_at'
        ]
    
    def get_assigned_to_name(self, obj):
        """Get assigned user name"""
        if obj.assigned_to:
            return obj.assigned_to.get_full_name()
        return None
    
    def get_approved_by_name(self, obj):
        """Get approver name"""
        if obj.approved_by:
            return obj.approved_by.get_full_name()
        return None
    
    def get_closed_by_name(self, obj):
        """Get closer name"""
        if obj.closed_by:
            return obj.closed_by.get_full_name()
        return None
    
    def get_sla_status(self, obj):
        """Get SLA status"""
        if obj.is_overdue:
            return 'overdue'
        
        from django.utils import timezone
        time_remaining = obj.due_at - timezone.now()
        hours_remaining = time_remaining.total_seconds() / 3600
        
        if hours_remaining < 4:
            return 'due_soon'
        return 'on_time'


class PXActionListSerializer(serializers.ModelSerializer):
    """Simplified PX Action serializer for list views"""
    hospital_name = serializers.CharField(source='hospital.name', read_only=True)
    assigned_to_name = serializers.SerializerMethodField()
    sla_status = serializers.SerializerMethodField()
    
    class Meta:
        model = PXAction
        fields = [
            'id', 'title', 'source_type', 'hospital_name',
            'category', 'severity', 'status', 'sla_status',
            'assigned_to_name', 'escalation_level', 'created_at'
        ]
    
    def get_assigned_to_name(self, obj):
        """Get assigned user name"""
        if obj.assigned_to:
            return obj.assigned_to.get_full_name()
        return None
    
    def get_sla_status(self, obj):
        """Get SLA status"""
        if obj.is_overdue:
            return 'overdue'
        
        from django.utils import timezone
        time_remaining = obj.due_at - timezone.now()
        hours_remaining = time_remaining.total_seconds() / 3600
        
        if hours_remaining < 4:
            return 'due_soon'
        return 'on_time'


class PXActionSLAConfigSerializer(serializers.ModelSerializer):
    """PX Action SLA configuration serializer"""
    hospital_name = serializers.CharField(source='hospital.name', read_only=True)
    department_name = serializers.CharField(source='department.name', read_only=True)
    
    class Meta:
        model = PXActionSLAConfig
        fields = [
            'id', 'name', 'hospital', 'hospital_name', 'department', 'department_name',
            'critical_hours', 'high_hours', 'medium_hours', 'low_hours',
            'reminder_hours_before', 'auto_escalate', 'escalation_delay_hours',
            'is_active', 'created_at', 'updated_at'
        ]
        read_only_fields = ['id', 'created_at', 'updated_at']


class RoutingRuleSerializer(serializers.ModelSerializer):
    """Routing rule serializer"""
    hospital_name = serializers.CharField(source='hospital.name', read_only=True)
    department_name = serializers.CharField(source='department.name', read_only=True)
    assign_to_user_name = serializers.SerializerMethodField()
    
    class Meta:
        model = RoutingRule
        fields = [
            'id', 'name', 'description', 'priority',
            'source_type', 'category', 'severity',
            'hospital', 'hospital_name', 'department', 'department_name',
            'assign_to_role', 'assign_to_user', 'assign_to_user_name', 'assign_to_department',
            'is_active', 'created_at', 'updated_at'
        ]
        read_only_fields = ['id', 'created_at', 'updated_at']
    
    def get_assign_to_user_name(self, obj):
        """Get assigned user name"""
        if obj.assign_to_user:
            return obj.assign_to_user.get_full_name()
        return None
