package redisotp

import (
	"context"
	"errors"

	richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
	"github.com/redis/go-redis/v9"
)

func (d DB) GetCodeByPhoneNumber(ctx context.Context, phoneNumber string) (string, error) {
	const op = "redisotp.GetCodeByPhoneNumber"

	value, err := d.adapter.Client().Get(ctx, phoneNumber).Result()
	if err != nil {
		rErr := redis.Nil
		if errors.As(err, &rErr) {
			return "", nil
		}

		return "", richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
	}

	return value, nil
}
