from rest_framework import serializers

from service import models
from service.service import PointService


class PlacementPointSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.PlacementPoint
        fields = '__all__'

    def to_representation(self, instance):
        representation = super().to_representation(instance)
        min_distances = PointService.get_min_distances_to_group(instance.id)
        representation['min_distance_to_group'] = min_distances
        return representation

class PrePlacementPointSerializer(PlacementPointSerializer):
    class Meta:
        model = models.PrePlacementPoint
        fields = '__all__'



class PostAndPVZGroupSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Post_and_pvzGroup
        fields = '__all__'


class PostAndPVZCategorySerializer(serializers.ModelSerializer):
    groups = PostAndPVZGroupSerializer(read_only=True, many=True)

    class Meta:
        model = models.Post_and_pvzCategory
        fields = '__all__'


class OtherObjectsGroupSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.OtherObjectsGroup
        fields = '__all__'


class OtherObjectsCategorySerializer(serializers.ModelSerializer):
    groups = OtherObjectsGroupSerializer(many=True, read_only=True)

    class Meta:
        model = models.OtherObjectsCategory
        fields = '__all__'


class RayonSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.AO
        fields = '__all__'


class AOSerializer(serializers.ModelSerializer):
    rayons = RayonSerializer(many=True, read_only=True)

    class Meta:
        model = models.AO
        fields = '__all__'
