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"
	"git.gocasts.ir/ebhomengo/niki/repository/mysql"
)

func (d *DB) RegisterEmptyingRequestForKindBox(ctx context.Context, kindBox entity.KindBox) error {
	const op = "mysqlkindbox.RegisterEmptyingRequest"

	query := `UPDATE kind_boxes 
			  SET return_address_id = ?, return_refer_time_id = ?, return_refer_date = ?, status = ?
			  WHERE id = ? AND benefactor_id = ? AND status = ? AND deleted_at IS NULL`
	//nolint
	stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyKindBoxRegisterEmptyingRequest, query)
	if err != nil {
		return richerror.New(op).WithErr(err).
			WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
	}

	_, err = stmt.ExecContext(ctx,
		kindBox.ReturnAddressID, kindBox.ReturnReferTimeID, kindBox.ReturnReferDate, kindBox.Status,
		kindBox.ID, kindBox.BenefactorID, entity.KindBoxDeliveredStatus)
	if err != nil {
		return richerror.New(op).WithErr(err).WithMessage(errmsg.ErrorMsgSomethingWentWrong).
			WithKind(richerror.KindUnexpected)
	}

	return nil
}
