// Code generated by hertz generator.

package user

import (
	"apis/biz/utils"
	"context"
	"github.com/BeroKiTeer/MyGoMall/common/kitex_gen/auth"
	"github.com/cloudwego/hertz/pkg/common/hlog"
	"log"
	"strconv"

	"apis/hertz_gen/api/user"
	"apis/rpc"
	user_kitex "github.com/BeroKiTeer/MyGoMall/common/kitex_gen/user"
	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/protocol/consts"
)

// UserGet .
// @router /api/user/{id} [GET]
func UserGet(ctx context.Context, c *app.RequestContext) {
	var err error
	var req user.UserGetReq
	err = c.BindAndValidate(&req)
	if err != nil {
		utils.SendErrResponse(ctx, c, consts.StatusBadRequest, err)
		return
	}
	//获取请求头的token
	token := c.Request.Header.Get("Authorization")
	if token == "" {
		utils.SendErrResponse(ctx, c, consts.StatusUnauthorized, err)
		return
	}
	//获取用户id
	rawID, err := rpc.AuthClient.DecodeToken(ctx, &auth.DecodeTokenReq{Token: token})
	if err != nil {
		utils.SendErrResponse(ctx, c, consts.StatusInternalServerError, err)
		return
	}
	//根据userid调用查询用户的函数
	resp, err := rpc.UserClient.GetUserInfo(ctx, &user_kitex.GetUserInfoReq{UserId: rawID.UserId})
	if err != nil {
		utils.SendErrResponse(ctx, c, consts.StatusServiceUnavailable, err)
		return
	}
	utils.SendSuccessResponse(ctx, c, consts.StatusOK, resp)
}

// UserDelete .
// @router /api/user/{id} [DELETE]
func UserDelete(ctx context.Context, c *app.RequestContext) {
	var err error
	var req user.UserDeleteReq
	err = c.BindAndValidate(&req)
	if err != nil {
		utils.SendErrResponse(ctx, c, consts.StatusBadRequest, err)
		return
	}
	//获取id（为字符串类型）
	rawId := c.Param("id")
	//将字符串的id类型转换为整数
	id, err := strconv.ParseInt(rawId, 10, 32)
	if err != nil {
		utils.SendErrResponse(ctx, c, consts.StatusInternalServerError, err) //id类型必须为整数
		return
	}

	resp, err := rpc.UserClient.DeleteUser(ctx, &user_kitex.DeleteUserReq{UserId: int32(id)})
	if err != nil {
		utils.SendErrResponse(ctx, c, consts.StatusServiceUnavailable, err)
		return
	}

	utils.SendSuccessResponse(ctx, c, consts.StatusOK, resp)
}

// UserUpdate .
// @router /api/user [PUT]
func UserUpdate(ctx context.Context, c *app.RequestContext) {
	var err error
	var req user.UserUpdateReq
	err = c.BindAndValidate(&req)
	if err != nil {
		utils.SendErrResponse(ctx, c, consts.StatusBadRequest, err)
		return
	}
	//将请求体的json格式绑定到req这一结构体中(即将hjson中的value赋值到req对应的字段)
	err = utils.BindJson(c, &req)
	if err != nil {
		utils.SendErrResponse(ctx, c, consts.StatusBadRequest, err)
		return
	}
	resp, err := rpc.UserClient.UpdateUser(ctx, &user_kitex.UpdateUserReq{
		Token:       req.Token,
		Email:       req.Email,
		Password:    req.Password,
		PhoneNumber: req.PhoneNumber,
		Address:     req.Address,
	})
	if err != nil {
		utils.SendErrResponse(ctx, c, consts.StatusServiceUnavailable, err)
		return
	}
	utils.SendSuccessResponse(ctx, c, consts.StatusOK, resp)
}

// UserLogin .
// @router /api/user/login [POST]
func UserLogin(ctx context.Context, c *app.RequestContext) {
	var err error
	var req user.UserLoginReq
	var resp *user_kitex.LoginResp
	err = c.BindAndValidate(&req)
	if err != nil {
		utils.SendErrResponse(ctx, c, consts.StatusBadRequest, err)
		return
	}
	//将请求体的json格式绑定到req这一结构体中(即将hjson中的value赋值到req对应的字段)
	err = utils.BindJson(c, &req)
	if err != nil {
		hlog.Error(err)
		utils.SendErrResponse(ctx, c, consts.StatusBadRequest, err)
		return
	}
	resp, err = rpc.UserClient.Login(ctx, &user_kitex.LoginReq{
		Email:    req.Email,
		Password: req.Password,
	})

	if err != nil {
		log.Printf("RPC Register error: %v", err)
		utils.SendErrResponse(ctx, c, consts.StatusServiceUnavailable, err)
		return
	}

	utils.SendSuccessResponse(ctx, c, consts.StatusOK, resp.Token)
}

// UserLogout .
// @router /api/user/logout [POST]
func UserLogout(ctx context.Context, c *app.RequestContext) {
	var err error
	var req user.UserLogoutReq
	err = c.BindAndValidate(&req)
	if err != nil {
		hlog.Error(err)
		utils.SendErrResponse(ctx, c, consts.StatusBadRequest, err)
		return
	}
	//获取请求头的token
	token := c.Request.Header.Get("Authorization")
	//获取用户id
	rawID, err := rpc.AuthClient.DecodeToken(ctx, &auth.DecodeTokenReq{Token: token})
	if err != nil {
		utils.SendErrResponse(ctx, c, consts.StatusInternalServerError, err)
		return
	}
	//获取token
	resp, err := rpc.UserClient.Logout(ctx, &user_kitex.LogoutReq{UserId: rawID.UserId})
	if err != nil {
		utils.SendErrResponse(ctx, c, consts.StatusServiceUnavailable, err)
		return
	}

	utils.SendSuccessResponse(ctx, c, consts.StatusOK, resp)
}

// UserRegister .
// @router /api/user/register [POST]
func UserRegister(ctx context.Context, c *app.RequestContext) {
	var err error
	var req user.UserRegisterReq
	err = c.BindAndValidate(&req)
	if err != nil {
		utils.SendErrResponse(ctx, c, consts.StatusBadRequest, err)
		return
	}
	//将请求体的json格式绑定到req这一结构体中(即将hjson中的value赋值到req对应的字段)
	err = utils.BindJson(c, &req)
	if err != nil {
		utils.SendErrResponse(ctx, c, consts.StatusBadRequest, err)
		return
	}
	log.Printf("Received register request: %+v", req)
	_, err = rpc.UserClient.Register(ctx, &user_kitex.RegisterReq{
		Email:           req.Email,
		Password:        req.Password,
		ConfirmPassword: req.ConfirmPassword,
	})
	if err != nil {
		log.Printf("RPC Register error: %v", err)
		utils.SendErrResponse(ctx, c, consts.StatusServiceUnavailable, err)
		return
	}

	utils.SendSuccessResponse(ctx, c, consts.StatusOK, string("注册成功！"))
}

// UserChangePassword .
// @router /api/user/changePassword [PUT]
func UserChangePassword(ctx context.Context, c *app.RequestContext) {
	var err error
	var req user.UserChangePasswordReq
	err = c.BindAndValidate(&req)
	if err != nil {
		utils.SendErrResponse(ctx, c, consts.StatusBadRequest, err)
		return
	}
	//获取请求头的token
	token := c.Request.Header.Get("Authorization")

	err = utils.BindJson(c, &req)
	if err != nil {
		utils.SendErrResponse(ctx, c, consts.StatusBadRequest, err)
		return
	}
	resp, err := rpc.UserClient.UpdateUser(ctx, &user_kitex.UpdateUserReq{
		Token:    token,
		Password: req.NewPassword},
	)
	if err != nil {
		hlog.Error(err)
		utils.SendErrResponse(ctx, c, consts.StatusServiceUnavailable, err)
		return
	}
	utils.SendSuccessResponse(ctx, c, consts.StatusOK, resp)
}
