package mysqlrefertime

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) Get(ctx context.Context, referTimeID uint) (entity.ReferTime, error) {
	const op = richerror.Op("mysqlrefertime.Get")

	query := `select * from refer_times where id = ?`
	//nolint
	stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyReferTimeGetByID, query)
	if err != nil {
		return entity.ReferTime{}, richerror.New(op).WithErr(err).
			WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
	}

	row := stmt.QueryRowContext(ctx, referTimeID)
	r, err := scanReferTime(row)
	if err != nil {
		return entity.ReferTime{}, richerror.New(op).WithErr(err).
			WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
	}

	return r, nil
}
