package forget

import (
	"context"
	"time"

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

// 重罢密码
func Reset(ctx context.Context, in *pb.ForgetResetRequest) (reply *pb.StatusReply, err error) {
	if in.Identity == "" || in.Password == "" {
		return nil, errcode.ErrInvalidArgument
	}

	pa, err := models.GetPassportAccountByField("identity", in.Identity)
	if err != nil {
		return nil, err
	}

	if pa.Status == vars.Status_Disable {
		return nil, errcode.ErrPermissionDenied
	}

	err = impl.DBService.Model(&models.PassportAccount{}).Where("identity = ? ", pa.Identity).Update("password", utils.Md5(in.Password)).Error
	if err != nil {
		printer.Error("Update passport account password by identity %+v error:%v", in.Identity, err)
		return nil, errcode.ErrDB
	}

	return &pb.StatusReply{
		Message: "OK",
		Timeseq: time.Now().UnixMilli(),
	}, nil
}
