from rest_framework import serializers

from .models import WorkRecord


class WorkRecordSerializer(serializers.ModelSerializer):
    work_type_display = serializers.CharField(source='get_work_type_display', read_only=True)
    delivery_plan_id = serializers.SerializerMethodField()
    delivery_plan_name = serializers.SerializerMethodField()

    class Meta:
        model = WorkRecord
        fields = [
            'id',
            'work_date',
            'work_type',
            'work_type_display',
            'title',
            'year',
            'auto_created',
            'delivery_trip',
            'delivery_plan_id',
            'delivery_plan_name',
            'spreading_session',
            'levee_work_session',
            'created_at',
            'updated_at',
        ]

    def get_delivery_plan_id(self, obj):
        if obj.delivery_trip_id:
            return obj.delivery_trip.delivery_plan_id
        return None

    def get_delivery_plan_name(self, obj):
        if obj.delivery_trip_id:
            return obj.delivery_trip.delivery_plan.name
        return None
