package mysqlkindbox

import (
	"context"
	"time"

	"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) ReturnKindBox(ctx context.Context, kindBoxID uint, serialNumber string) error {
	const op = "mysqlkindbox.ReturnKindBox"

	query := `update kind_boxes set serial_number = ?, status = ?, returned_at = ? where id = ?`
	//nolint
	stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyKindBoxReturn, query)
	if err != nil {
		return richerror.New(op).WithErr(err).
			WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
	}

	_, err = stmt.ExecContext(ctx, serialNumber, entity.KindBoxReturnedStatus, time.Now(), kindBoxID)
	if err != nil {
		return richerror.New(op).WithErr(err).
			WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
	}

	return nil
}
