from django.contrib.auth.models import User, Group
from rest_framework import serializers

from.models import Vehicles_infos, Vehicles

class CustomDateField(serializers.DateField):
    def to_representation(self, value):
        return value.strftime('%d.%m.%Y')

class cl_Vehicles_infos_Serializer(serializers.ModelSerializer):
    start_formated = serializers.SerializerMethodField()
    author_formated = serializers.SerializerMethodField()

    class Meta:
        model = Vehicles_infos
        fields = '__all__'
    
    def get_start_formated(self, obj):
        return obj.get_Start_formated()

    def get_author_formated(self, obj):
        return obj.get_Author_formated()

class cl_Vehicles_Serializer(serializers.ModelSerializer):
    technique_infos = serializers.SerializerMethodField()
    matos_infos = serializers.SerializerMethodField()
    perempt_infos_fixes = serializers.SerializerMethodField()
    perempt_infos = serializers.SerializerMethodField()

    
    class Meta:
        model = Vehicles
        fields = '__all__'
    
    def get_technique_infos(self, obj):
        enabled_infos = Vehicles_infos.objects.filter(Vehicle=obj, bEnabled=True, nType=1).order_by('-dtCreated')
        return cl_Vehicles_infos_Serializer(enabled_infos, many=True).data
    def get_matos_infos(self, obj):
        enabled_infos = Vehicles_infos.objects.filter(Vehicle=obj, bEnabled=True, nType=2).order_by('-dtCreated')
        return cl_Vehicles_infos_Serializer(enabled_infos, many=True).data
    def get_perempt_infos_fixes(self, obj):
        enabled_infos = Vehicles_infos.objects.filter(Vehicle=obj, bEnabled=True, nType=3)[:3]
        return cl_Vehicles_infos_Serializer(enabled_infos, many=True).data
    def get_perempt_infos(self, obj):
        enabled_infos = Vehicles_infos.objects.filter(Vehicle=obj, bEnabled=True, nType=3)[3:]
        return cl_Vehicles_infos_Serializer(enabled_infos, many=True).data
