package agentkindboxservice

import (
	"context"

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

func (s Service) AddBatchKindBox(ctx context.Context, req param.AddKindBoxRequest) (param.AddKindBoxResponse, error) {
	const op = "agentkindboxservice.AddKindBox"

	kindBoxes := make([]entity.KindBox, 0)
	for i := 0; i < int(req.CountAccepted); i++ {
		kb := entity.KindBox{
			KindBoxReqID:       req.KindBoxReqID,
			BenefactorID:       req.BenefactorID,
			KindBoxType:        req.Type,
			DeliverReferTimeID: req.DeliverReferTimeID,
			DeliverReferDate:   req.DeliverReferDate,
			DeliverAddressID:   req.DeliverAddressID,
			DeliveredAt:        req.DeliveredAt,
			SenderAgentID:      req.SenderAgentID,
			Status:             entity.KindBoxDeliveredStatus,
		}
		if i < len(req.SerialNumbers) {
			kb.SerialNumber = req.SerialNumbers[i]
		}
		kindBoxes = append(kindBoxes, kb)
	}
	aErr := s.repo.AddBatchKindBox(ctx, kindBoxes)
	if aErr != nil {
		return param.AddKindBoxResponse{}, richerror.New(op).WithErr(aErr).WithKind(richerror.KindUnexpected)
	}

	return param.AddKindBoxResponse{}, nil
}
