
package mysqlkindboxreq

import (
	"context"

	entity "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) UpdateKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) (entity.KindBoxReq, error) {

	const op = "mysqlkindboxreq.UpdateKindBoxReq"


	query := `UPDATE kind_box_reqs SET 
		kind_box_type = ?, count_requested = ?, description = ?, deliver_refer_time_id = ?,
		deliver_refer_date = ?, deliver_address_id = ? 
		WHERE id = ? AND benefactor_id = ? AND deleted_at IS NULL`


	_, uErr := d.conn.Conn().ExecContext(ctx, query,
	 kindBoxReq.KindBoxType, kindBoxReq.CountRequested,
	 kindBoxReq.Description, kindBoxReq.DeliverReferTimeID, kindBoxReq.DeliverReferDate,
	 kindBoxReq.DeliverAddressID, kindBoxReq.ID, kindBoxReq.BenefactorID)


	if uErr != nil {
		return entity.KindBoxReq{}, richerror.New(op).WithErr(uErr).WithMessage(errmsg.ErrorMsgCantUpdateRecord).
		WithKind(richerror.KindUnexpected)

	}

	updatedKindBoxReq, gErr := d.GetKindBoxReqByID(ctx, kindBoxReq.ID)
	if gErr != nil {
		return entity.KindBoxReq{}, richerror.New(op).WithErr(gErr).WithMessage(errmsg.ErrorMsgNotFound).
		WithKind(richerror.KindUnexpected)
	}

	return updatedKindBoxReq, nil
}