// Code generated by hertz generator.

package tiktokapi

import (
	"context"
	"github.com/PICOF/simple-tiktok/cmd/api/biz/model/tiktokapi"
	"github.com/PICOF/simple-tiktok/cmd/api/biz/rpc/user"
	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/protocol/consts"
)

// UserLogin .
// @router /douyin/user/login/ [POST]
func UserLogin(ctx context.Context, c *app.RequestContext) {
	var err error
	var req tiktokapi.LoginRequest
	err = c.BindAndValidate(&req)
	if err != nil {
		c.String(consts.StatusBadRequest, err.Error())
		return
	}
	resp, err := user.Login(ctx, &req)

	if err != nil && resp == nil {
		msg := "请求远程服务时出错"
		c.JSON(consts.StatusInternalServerError, tiktokapi.LoginResponse{
			StatusCode: -1,
			StatusMsg:  &msg,
		})
	} else {
		c.JSON(consts.StatusOK, resp)
	}
}

// GetUserInfo .
// @router /douyin/user/ [GET]
func GetUserInfo(ctx context.Context, c *app.RequestContext) {
	var err error
	var req tiktokapi.UserInfoRequest
	err = c.BindAndValidate(&req)
	if err != nil {
		c.String(consts.StatusBadRequest, err.Error())
		return
	}

	resp, err := user.GetInfo(ctx, c.GetInt64("user_id"))

	if err != nil && resp == nil {
		msg := "请求远程服务时出错"
		c.JSON(consts.StatusInternalServerError, tiktokapi.LoginResponse{
			StatusCode: -1,
			StatusMsg:  &msg,
		})
	} else {
		c.JSON(consts.StatusOK, resp)
	}
}

// UserRegister .
// @router /douyin/user/register/ [POST]
func UserRegister(ctx context.Context, c *app.RequestContext) {
	var err error
	var req tiktokapi.RegisterRequest
	err = c.BindAndValidate(&req)
	if err != nil {
		c.String(consts.StatusBadRequest, err.Error())
		return
	}

	resp, err := user.Register(ctx, &req)

	if err != nil && resp == nil {
		msg := "请求远程服务时出错"
		c.JSON(consts.StatusInternalServerError, tiktokapi.FeedResponse{
			StatusCode: -1,
			StatusMsg:  &msg,
			NextTime:   nil,
			VideoList:  nil,
		})
	} else {
		c.JSON(consts.StatusOK, resp)
	}
}
