package benefactorkindboxreqservice

import (
	"context"

	param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req"
	richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)

func (s Service) Update(ctx context.Context, req param.KindBoxReqUpdateRequest) (param.KindBoxReqUpdateResponse, error) {
	const op = "benefactorkindboxreqservice.Update"
	if fieldErrors, vErr := s.vld.ValidateUpdateRequest(ctx, req); vErr != nil {
		return param.KindBoxReqUpdateResponse{FieldErrors: fieldErrors}, richerror.New(op).WithErr(vErr)
	}

	kindBoxReq, err := s.repo.GetKindBoxReqByID(ctx, req.KindBoxReqID)
	if err != nil {
		return param.KindBoxReqUpdateResponse{}, richerror.New(op).WithErr(err)
	}

	kindBoxReq.KindBoxType = req.KindBoxType
	kindBoxReq.CountRequested = req.CountRequested
	kindBoxReq.Description = req.Description
	kindBoxReq.DeliverReferTimeID = req.DeliverReferTimeID
	kindBoxReq.DeliverReferDate = req.DeliverReferDate
	kindBoxReq.DeliverAddressID = req.DeliverAddressID

	uErr := s.repo.UpdateKindBoxReq(ctx, kindBoxReq)
	if uErr != nil {
		return param.KindBoxReqUpdateResponse{}, richerror.New(op).WithErr(err)
	}

	return param.KindBoxReqUpdateResponse{}, nil
}
