"""
Journeys serializers
"""
from rest_framework import serializers

from .models import (
    PatientJourneyInstance,
    PatientJourneyStageInstance,
    PatientJourneyStageTemplate,
    PatientJourneyTemplate,
)


class PatientJourneyStageTemplateSerializer(serializers.ModelSerializer):
    """Journey stage template serializer"""
    survey_template_name = serializers.CharField(source='survey_template.name', read_only=True)
    
    class Meta:
        model = PatientJourneyStageTemplate
        fields = [
            'id', 'journey_template', 'name', 'name_ar', 'code', 'order',
            'trigger_event_code', 'survey_template', 'survey_template_name',
            'is_optional', 'is_active',
            'created_at', 'updated_at'
        ]
        read_only_fields = ['id', 'created_at', 'updated_at']


class PatientJourneyTemplateSerializer(serializers.ModelSerializer):
    """Journey template serializer"""
    hospital_name = serializers.CharField(source='hospital.name', read_only=True)
    stages = PatientJourneyStageTemplateSerializer(many=True, read_only=True)
    stage_count = serializers.SerializerMethodField()
    
    class Meta:
        model = PatientJourneyTemplate
        fields = [
            'id', 'name', 'name_ar', 'journey_type', 'description',
            'hospital', 'hospital_name', 'is_active', 'is_default',
            'stages', 'stage_count',
            'created_at', 'updated_at'
        ]
        read_only_fields = ['id', 'created_at', 'updated_at']
    
    def get_stage_count(self, obj):
        """Get number of stages"""
        return obj.stages.count()


class PatientJourneyStageInstanceSerializer(serializers.ModelSerializer):
    """Journey stage instance serializer"""
    stage_name = serializers.CharField(source='stage_template.name', read_only=True)
    stage_order = serializers.IntegerField(source='stage_template.order', read_only=True)
    staff_name = serializers.SerializerMethodField()
    department_name = serializers.CharField(source='department.name', read_only=True)
    
    class Meta:
        model = PatientJourneyStageInstance
        fields = [
            'id', 'journey_instance', 'stage_template', 'stage_name', 'stage_order',
            'status', 'completed_at',
            'staff', 'staff_name', 'department', 'department_name',
            'metadata', 'created_at', 'updated_at'
        ]
        read_only_fields = [
            'id', 'completed_at',
            'created_at', 'updated_at'
        ]
    
    def get_staff_name(self, obj):
        """Get staff full name"""
        if obj.staff:
            return obj.staff.get_full_name()
        return None


class PatientJourneyInstanceSerializer(serializers.ModelSerializer):
    """Journey instance serializer"""
    journey_template_name = serializers.CharField(source='journey_template.name', read_only=True)
    journey_type = serializers.CharField(source='journey_template.journey_type', read_only=True)
    patient_name = serializers.CharField(source='patient.get_full_name', read_only=True)
    patient_mrn = serializers.CharField(source='patient.mrn', read_only=True)
    hospital_name = serializers.CharField(source='hospital.name', read_only=True)
    department_name = serializers.CharField(source='department.name', read_only=True)
    stage_instances = PatientJourneyStageInstanceSerializer(many=True, read_only=True)
    completion_percentage = serializers.SerializerMethodField()
    is_complete = serializers.SerializerMethodField()
    
    class Meta:
        model = PatientJourneyInstance
        fields = [
            'id', 'journey_template', 'journey_template_name', 'journey_type',
            'patient', 'patient_name', 'patient_mrn', 'encounter_id',
            'hospital', 'hospital_name', 'department', 'department_name',
            'status', 'started_at', 'completed_at',
            'stage_instances', 'completion_percentage', 'is_complete',
            'metadata', 'created_at', 'updated_at'
        ]
        read_only_fields = [
            'id', 'started_at', 'completed_at',
            'created_at', 'updated_at'
        ]
    
    def get_completion_percentage(self, obj):
        """Get completion percentage"""
        return obj.get_completion_percentage()
    
    def get_is_complete(self, obj):
        """Check if journey is complete"""
        return obj.is_complete()


class PatientJourneyInstanceListSerializer(serializers.ModelSerializer):
    """Simplified journey instance serializer for list views"""
    journey_template_name = serializers.CharField(source='journey_template.name', read_only=True)
    journey_type = serializers.CharField(source='journey_template.journey_type', read_only=True)
    patient_name = serializers.CharField(source='patient.get_full_name', read_only=True)
    patient_mrn = serializers.CharField(source='patient.mrn', read_only=True)
    hospital_name = serializers.CharField(source='hospital.name', read_only=True)
    completion_percentage = serializers.SerializerMethodField()
    
    class Meta:
        model = PatientJourneyInstance
        fields = [
            'id', 'encounter_id', 'journey_template_name', 'journey_type',
            'patient_name', 'patient_mrn', 'hospital_name',
            'status', 'completion_percentage', 'started_at'
        ]
    
    def get_completion_percentage(self, obj):
        """Get completion percentage"""
        return obj.get_completion_percentage()
