from rest_framework import serializers

from correlation.models import Group, Rule


class RuleRelatedField(serializers.PrimaryKeyRelatedField):

    def to_representation(self, rule: Rule):
        return IDNameRuleSerializer(rule).data


class IDNameRuleSerializer(serializers.ModelSerializer):
    class Meta:
        model = Rule
        fields = ['id', 'name']


class RuleGroupRelatedField(serializers.PrimaryKeyRelatedField):

    def to_representation(self, group: Group):
        return IDNameGroupSerializer(group).data

    def use_pk_only_optimization(self):
        return False


class IDNameGroupSerializer(serializers.ModelSerializer):
    class Meta:
        model = Group
        fields = ['id', 'name']
