package benefactorkindboxservice

import (
	"context"

	param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box"
	response "git.gocasts.ir/ebhomengo/niki/pkg/response_builder"
	richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)

func (s Service) Get(ctx context.Context, req param.KindBoxGetRequest) (param.KindBoxGetResponse, error) {
	const op = "userkindboxservice.Get"
	if fieldErrors, vErr := s.vld.ValidateGetRequest(ctx, req); vErr != nil {
		return param.KindBoxGetResponse{FieldErrors: fieldErrors}, richerror.New(op).WithErr(vErr)
	}
	kindBox, err := s.repo.GetKindBox(ctx, req.KindBoxID)
	if err != nil {
		return param.KindBoxGetResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
	}

	return param.KindBoxGetResponse{Data: param.Data{
		ID:                 kindBox.ID,
		KindBoxReqID:       kindBox.KindBoxReqID,
		BenefactorID:       kindBox.BenefactorID,
		KindBoxType:        kindBox.KindBoxType,
		Amount:             kindBox.Amount,
		SerialNumber:       kindBox.SerialNumber,
		Status:             kindBox.Status,
		DeliverReferTimeID: kindBox.DeliverReferTimeID,
		DeliverReferDate:   kindBox.DeliverReferDate,
		DeliverAddressID:   kindBox.DeliverAddressID,
		SenderAgentID:      kindBox.SenderAgentID,
		DeliveredAt:        kindBox.DeliveredAt,
		ReturnReferTimeID:  kindBox.ReturnReferTimeID,
		ReturnReferDate:    response.GetNullDate(kindBox.ReturnReferDate),
		ReturnAddressID:    kindBox.ReturnAddressID,
		ReceiverAgentID:    kindBox.ReceiverAgentID,
		ReturnedAt:         response.GetNullDate(kindBox.ReturnedAt),
	}}, nil
}
