package adminkindboxreqservice

import (
	"context"

	param "git.gocasts.ir/ebhomengo/niki/param/admin/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 = "adminkindboxreqservice.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.GetByID(ctx, req.ID)
	if err != nil {
		return param.KindBoxReqUpdateResponse{}, richerror.New(op).WithErr(err)
	}

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

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

	return param.KindBoxReqUpdateResponse{}, nil
}
