package mysqlkindboxreq

import (
	"context"
	"time"

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

func (d *DB) DeliverKindBoxReq(ctx context.Context, kindBoxReqID uint) error {
	const op = "mysqlkindboxreq.DeliverKindBoxReq"
	q, cErr := querier.GetQuerierFromContextOrNew(ctx).Continue(ctx, d.conn.Conn())
	if cErr != nil {
		return richerror.New(op).WithErr(cErr).
			WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
	}
	_, uErr := q.Conn().ExecContext(ctx, `update kind_box_reqs set  status = ?, delivered_at = ?  where id = ?`,
		entity.KindBoxReqDeliveredStatus, time.Now(), kindBoxReqID)
	if uErr != nil {
		return richerror.New(op).WithErr(uErr).
			WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
	}

	return nil
}
