package user

import (
	"context"

	v1 "cris/api/user/v1"
	"cris/internal/model/do"
	"cris/internal/service"

	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
)

type sUser struct{}

func init() {
	service.RegisterUser(&sUser{})
}

func (s *sUser) Register(ctx context.Context, req *v1.UserRegisterReq) error {
	// 两次密码不一致
	if req.ConfirmPassword != req.Password {
		return gerror.New("invalid password")
	}

	m := g.Model("user")
	count, err := m.Where("email=?", req.Email).Count()
	if err != nil {
		return err
	}
	if count > 0 {
		return gerror.New("Email is already in use")
	}

	var user do.User
	err = gconv.Struct(req, &user)
	if err != nil {
		return gerror.New("register failed")
	}
	result, err := m.Data(user).Insert()
	if err != nil {
		return gerror.New("register failed1")
	}

	if result == nil {
		return gerror.New("register failed2")
	}
	return nil
}

func (s *sUser) Login(ctx context.Context, req *v1.UserLoginReq) error {
	// 在这里进行具体的逻辑处理
	return nil
}
