package mysqlkindbox

import (
	"context"

	"git.gocasts.ir/ebhomengo/niki/entity"
	errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
	richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)

func (d DB) AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) error {
	const op = "mysqlkindbox.AddBatchKindBoxConcurrentlyRollback"

	queryStr := "INSERT INTO kind_boxes (kind_box_req_id, benefactor_id, type, serial_number, status) VALUES "
	values := []any{}

	for _, kb := range kindBoxes {
		queryStr += "(?, ?, ?, ?, ?),"
		values = append(values, kb.KindBoxReqID, kb.BenefactorID, kb.Type, kb.SerialNumber, kb.Status.String())
	}

	// trim the last ,
	queryStr = queryStr[0 : len(queryStr)-1]

	if _, err := d.conn.Conn().ExecContext(ctx, queryStr, values...); err != nil {
		return richerror.New(op).WithErr(err).
			WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
	}

	return nil
}
