package sms

import (
	"context"

	"bsm/full/module/base/sender/internal/excode"
	"bsm/full/module/base/sender/internal/impl"
	pb "bsm/full/module/base/sender/pb"
	"git.apinb.com/bsm-sdk/core/errcode"
)

func Verify(ctx context.Context, in *pb.SmsVerifyRequest) (reply *pb.SmsReply, err error) {
	if in.GetCode() == "" {
		return nil, excode.ErrCode
	}

	if in.GetPhone() == "" {
		return nil, excode.ErrPhone
	}

	//check redis
	key := KeyPrefix + in.GetPhone()
	code, err := impl.RedisService.Client.Get(impl.RedisService.Ctx, key).Result()
	if err != nil {
		return nil, errcode.NewError(1311, err.Error())
	}
	//check code
	if code == in.Code {
		return &pb.SmsReply{
			Reply: "true",
		}, nil
	}

	//verify pass ; delete the requestId
	impl.RedisService.Client.Del(impl.RedisService.Ctx, key)

	return &pb.SmsReply{
		Reply: "false",
	}, nil
}
