package benefactorkindboxservice

import (
	"context"

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

func (s Service) RegisterEmptyingRequest(ctx context.Context, req param.KindBoxRegisterEmptyingRequest) (param.KindBoxRegisterEmptyingRequestResponse, error) {
	const op = "benefactorKindBoxService.RegisterEmptyingRequest"
	if fieldErrors, vErr := s.vld.ValidateRegisterEmptyingRequest(ctx, req); vErr != nil {
		return param.KindBoxRegisterEmptyingRequestResponse{FieldErrors: fieldErrors}, richerror.New(op).WithErr(vErr)
	}

	rErr := s.repo.RegisterEmptyingRequestForKindBox(ctx, entity.KindBox{
		ID:                req.KindBoxID,
		ReturnAddressID:   req.ReturnAddressID,
		ReturnReferTimeID: req.ReturnReferTimeID,
		ReturnReferDate:   req.ReturnReferDate,
		Status:            entity.KindBoxReadyToReturnStatus,
		BenefactorID:      req.BenefactorID,
	})
	if rErr != nil {
		return param.KindBoxRegisterEmptyingRequestResponse{}, richerror.New(op).WithErr(rErr)
	}

	return param.KindBoxRegisterEmptyingRequestResponse{}, nil
}
