package agentkindboxreqservice

import (
	"context"

	param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box_req"
	response "git.gocasts.ir/ebhomengo/niki/pkg/response_builder"
	richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)

func (s Service) GetAwaitingDelivery(ctx context.Context, req param.DeliveryAwaitingGetRequest) (param.DeliveryAwaitingGetResponse, error) {
	const op = "agentkindboxreqservice.GetAwaitingDelivery"
	if fieldErrors, vErr := s.vld.ValidateGetAwaitingDeliveryRequest(req); vErr != nil {
		return param.DeliveryAwaitingGetResponse{FieldErrors: fieldErrors}, richerror.New(op).WithErr(vErr)
	}
	kindBoxReq, err := s.repo.GetAwaitingDeliveryByAgent(ctx, req.KindBoxReqID, req.AgentID)
	if err != nil {
		return param.DeliveryAwaitingGetResponse{}, richerror.New(op).WithErr(err)
	}

	return param.DeliveryAwaitingGetResponse{
		Data: param.Data{
			ID:                 kindBoxReq.ID,
			BenefactorID:       kindBoxReq.BenefactorID,
			KindBoxType:        kindBoxReq.KindBoxType,
			CountRequested:     kindBoxReq.CountRequested,
			CountAccepted:      kindBoxReq.CountAccepted,
			Description:        kindBoxReq.Description,
			Status:             kindBoxReq.Status,
			DeliverReferTimeID: kindBoxReq.DeliverReferTimeID,
			DeliverReferDate:   kindBoxReq.DeliverReferDate,
			DeliverAddressID:   kindBoxReq.DeliverAddressID,
			SenderAgentID:      kindBoxReq.SenderAgentID,
			DeliveredAt:        response.GetNullDate(kindBoxReq.DeliveredAt),
		},
	}, nil
}
