from apps.mtm.models import Step
from rest_framework import serializers
from rest_framework.serializers import ModelSerializer

from .models import Equipment,Equipmentrecord
from apps.system.serializers import OrganizationSimpleSerializer, UserSimpleSerializer


class EquipmentSerializer(ModelSerializer):
    belong_dept_ = OrganizationSimpleSerializer(source='belong_dept', read_only=True)
    keeper_ = UserSimpleSerializer(source='keeper', read_only=True)
    step_ = serializers.SerializerMethodField()
    class Meta:
        model = Equipment
        fields = '__all__'

    @staticmethod
    def setup_eager_loading(queryset):
        """ Perform necessary eager loading of data. """
        queryset = queryset.select_related('belong_dept','keeper')
        return queryset

    def get_step_(self, obj):
        return Step.objects.filter(equipments=obj).values('id', 'name', 'number')


class EquipmentSimpleSerializer(ModelSerializer):
    class Meta:
        model = Equipment
        fields = ['id', 'number', 'name']



class EquipmentrecordSerializer(ModelSerializer):
    equipment_ = EquipmentSerializer(source='equipment', read_only=True)
    class Meta:
        model = Equipmentrecord
        fields = '__all__'

    @staticmethod
    def setup_eager_loading(queryset):
        """ Perform necessary eager loading of data. """
        queryset = queryset.select_related('equipment')
        return queryset

class DaqCreateSerializer(serializers.Serializer):
    number = serializers.CharField()
    file = serializers.FileField()