from django.contrib.auth.models import User

from core import serializers
from incident.models import Incident, IncidentCategory, IncidentEffect, IncidentRecommendations


class IncidentEffectSerializer(serializers.ModelSerializer):
    class Meta:
        model = IncidentEffect
        fields = ['id', 'name', 'description']


class IncidentRecommendationsSerializer(serializers.ModelSerializer):  # todo delete
    class Meta:
        model = IncidentRecommendations
        fields = ['id', 'name', 'description']


class IncidentCategorySerializer(serializers.ModelLocalizedSerializer):
    """Incident Category incident for IncidentSerializer"""

    class Meta:
        model = IncidentCategory
        fields = ['name', 'description', 'id']


class IncidentSerializer(serializers.ModelLocalizedSerializer):
    """Main Incident Serializer for ViewSet"""
    category = IncidentCategorySerializer()
    assigned_to = serializers.StringRelatedField()
    events = serializers.JSONField()
    notification_id = serializers.SerializerMethodField(read_only=True)
    close_recommendations = IncidentRecommendationsSerializer(many=True)
    effects = IncidentEffectSerializer(many=True)

    def get_notification_id(self, incident: Incident) -> int:
        context = self.context.get('incident_notification_id_map', {})
        notification_id = context.get(str(incident.pk))
        return notification_id

    class Meta:
        model = Incident
        fields = ['user_friendly_id', 'timestamp', 'incident_id', 'title', 'category', 'importance', 'status',
                  'assigned_to', 'event_count', 'created', 'updated', 'deadline', 'comment', 'events',
                  'notification_id', 'close_recommendations', 'effects']


class IncidentsCsvExportSerializer(serializers.ModelLocalizedSerializer):
    """ Serializer for CSV export of Incidents data
    The idea behind parsing almost every field is that if we dont do this, than fields that are used as links to the
        other models will be shown in table as the dictionary
    """
    status = serializers.ReadOnlyField(source='get_status_display', allow_null=False)
    category = serializers.ReadOnlyField(source='category.name', allow_null=True)
    assigned_to = serializers.ReadOnlyField(source='obj.assigned_to.username', allow_null=True)
    events = serializers.JSONField()

    class Meta:
        model = Incident
        fields = ['user_friendly_id', 'timestamp', 'title', 'category', 'importance', 'status', 'assigned_to',
                  'event_count', 'created', 'updated', 'description', 'deadline', 'comment', 'events']


class DumpIncidentSerializer(serializers.ModelSerializer):
    """Serializer for dupm full incident data in `rotate_incidents` data."""

    class DumpIncidentCategorySerializer(serializers.ModelSerializer):
        class Meta:
            model = IncidentCategory
            fields = '__all__'

    class DumpIncidentEffectSerializer(serializers.ModelSerializer):
        class Meta:
            model = IncidentEffect
            fields = '__all__'

    class DumpIncidentRecommendationsSerializer(serializers.ModelSerializer):
        class Meta:
            model = IncidentRecommendations
            fields = '__all__'

    class DumpIncidentAssignedToSerializer(serializers.ModelSerializer):
        class Meta:
            model = User
            fields = ('id', 'username', 'first_name')

    category = DumpIncidentCategorySerializer()
    effects = DumpIncidentEffectSerializer(many=True)
    close_recommendations = DumpIncidentRecommendationsSerializer(many=True)
    assigned_to = DumpIncidentAssignedToSerializer()

    class Meta:
        model = Incident
        fields = '__all__'
