package register

import (
	"context"
	"regexp"

	"bsm/full/module/base/passport/internal/models"
	pb "bsm/full/module/base/passport/pb"
	"git.apinb.com/bsm-sdk/core/errcode"
)

// 手机验证码注册
func Code(ctx context.Context, in *pb.RegisterRequest) (reply *pb.RegisterReply, err error) {
	if in.Phone == "" || in.Code == "" {
		return nil, errcode.ErrInvalidArgument
	}

	//手机号验证
	matched, err := regexp.MatchString("^1[3456789]{1}\\d{9}$", in.Phone)
	if err != nil || !matched {
		return nil, errcode.ErrInvalidArgument
	}

	//账号唯一验证
	found := models.PassportAccountExists("phone", in.Phone)
	if found {
		return nil, errcode.ErrAlreadyExists
	}

	return Do(in)
}
