// Code generated by hertz generator.

package admin

import (
	"context"
	admin2 "formulago/biz/domain/admin"
	logic "formulago/biz/logic/admin"
	"formulago/data"
	"github.com/cloudwego/hertz/pkg/protocol/consts"
	"github.com/jinzhu/copier"
	"strconv"

	"formulago/api/model/admin"
	base "formulago/api/model/base"
	"github.com/cloudwego/hertz/pkg/app"
)

// Register .
// @router /api/register [POST]
func Register(ctx context.Context, c *app.RequestContext) {
	var err error
	var req admin.RegisterReq
	resp := new(base.BaseResp)
	err = c.BindAndValidate(&req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusBadRequest, resp)
		return
	}

	resp.ErrMsg = "Register are not supported"
	resp.ErrCode = base.ErrCode_Fail
	c.JSON(consts.StatusOK, resp)
}

// UserPermCode .
// @router /api/admin/user/perm [GET]
func UserPermCode(ctx context.Context, c *app.RequestContext) {
	var err error
	var req base.Empty
	resp := new(admin.PermCodeResp)
	err = c.BindAndValidate(&req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusBadRequest, resp)
		return
	}

	roleID, exist := c.Get("roleID")
	if !exist || roleID == nil {
		c.JSON(consts.StatusUnauthorized, "Unauthorized")
		return
	}
	resp.Data = []string{roleID.(string)}
	resp.ErrCode = base.ErrCode_Success
	resp.ErrMsg = "success"
	c.JSON(consts.StatusOK, resp)
}

// ChangePassword .
// @router /api/admin/user/change-password [POST]
func ChangePassword(ctx context.Context, c *app.RequestContext) {
	var err error
	var req admin.ChangePasswordReq
	resp := new(base.BaseResp)
	err = c.BindAndValidate(&req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusBadRequest, resp)
		return
	}

	err = logic.NewUser(data.Default()).ChangePassword(ctx, req.UserID, req.OldPassword, req.NewPassword)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}

	resp.ErrCode = base.ErrCode_Success
	resp.ErrMsg = "success"
	c.JSON(consts.StatusOK, resp)
}

// CreateUser .
// @router /api/admin/user/create [POST]
func CreateUser(ctx context.Context, c *app.RequestContext) {
	var err error
	var req admin.CreateOrUpdateUserReq
	resp := new(base.BaseResp)
	err = c.BindAndValidate(&req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusBadRequest, resp)
		return
	}

	err = logic.NewUser(data.Default()).Create(ctx, admin2.CreateOrUpdateUserReq{
		Username: req.Username,
		Password: req.Password,
		Email:    req.Email,
		Mobile:   req.Mobile,
		RoleID:   req.RoleID,
		Avatar:   req.Avatar,
		Nickname: req.Nickname,
		Status:   req.Status,
	})

	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}

	resp.ErrCode = base.ErrCode_Success
	resp.ErrMsg = "success"
	c.JSON(consts.StatusOK, resp)
}

// UpdateUser .
// @router /api/admin/user/update [POST]
func UpdateUser(ctx context.Context, c *app.RequestContext) {
	var err error
	var req admin.CreateOrUpdateUserReq
	resp := new(base.BaseResp)
	err = c.BindAndValidate(&req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusBadRequest, resp)
		return
	}

	err = logic.NewUser(data.Default()).Update(ctx, admin2.CreateOrUpdateUserReq{
		ID:       req.ID,
		Username: req.Username,
		Password: req.Password,
		Email:    req.Email,
		Mobile:   req.Mobile,
		RoleID:   req.RoleID,
		Avatar:   req.Avatar,
		Nickname: req.Nickname,
		Status:   req.Status,
	})
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}

	resp.ErrCode = base.ErrCode_Success
	resp.ErrMsg = "success"
	c.JSON(consts.StatusOK, resp)
}

// UserInfo .
// @router /api/admin/user/info [GET]
func UserInfo(ctx context.Context, c *app.RequestContext) {
	var err error
	var req base.Empty
	resp := new(admin.UserInfoResp)
	err = c.BindAndValidate(&req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusBadRequest, resp)
		return
	}

	v, exist := c.Get("userID")
	if !exist || v == nil {
		c.JSON(consts.StatusUnauthorized, "Unauthorized")
		return
	}
	i, err := strconv.Atoi(v.(string))
	if err != nil {
		c.JSON(consts.StatusUnauthorized, "Unauthorized,"+err.Error())
		return
	}
	userID := uint64(i)
	// get user info
	user, err := logic.NewUser(data.Default()).UserInfo(ctx, userID)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}
	resp.ID = user.ID
	resp.Username = user.Username
	resp.Status = uint64(user.Status)
	resp.Email = user.Email
	resp.Mobile = user.Mobile
	resp.RoleID = user.RoleID
	resp.Avatar = user.Avatar
	resp.Nickname = user.Nickname
	resp.CreatedAt = user.CreatedAt.Format("2006-01-02 15:04:05")
	resp.UpdatedAt = user.UpdatedAt.Format("2006-01-02 15:04:05")
	resp.SideMode = user.SideMode
	resp.RoleName = user.RoleName
	resp.RoleValue = user.RoleValue

	resp.ErrCode = base.ErrCode_Success
	resp.ErrMsg = "success"
	c.JSON(consts.StatusOK, resp)
}

// UserList .
// @router /api/admin/user/list [GET]
func UserList(ctx context.Context, c *app.RequestContext) {
	var err error
	var req admin.UserListReq
	resp := new(admin.UserListResp)
	err = c.BindAndValidate(&req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusBadRequest, resp)
		return
	}

	var userListReq admin2.UserListReq
	err = copier.Copy(&userListReq, &req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}
	userList, total, err := logic.NewUser(data.Default()).List(ctx, userListReq)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}
	for _, v := range userList {
		resp.Data = append(resp.Data, &admin.UserInfoResp{
			ID:        v.ID,
			Avatar:    v.Avatar,
			RoleID:    v.RoleID,
			Mobile:    v.Mobile,
			Email:     v.Email,
			Status:    uint64(v.Status),
			Username:  v.Username,
			Nickname:  v.Nickname,
			CreatedAt: v.CreatedAt.Format("2006-01-02 15:04:05"),
			UpdatedAt: v.UpdatedAt.Format("2006-01-02 15:04:05"),
		})
	}
	resp.Total = uint64(total)
	resp.ErrCode = base.ErrCode_Success
	resp.ErrMsg = "success"
	c.JSON(consts.StatusOK, resp)
}

// DeleteUser .
// @router /api/admin/user [DELETE]
func DeleteUser(ctx context.Context, c *app.RequestContext) {
	var err error
	var req base.IDReq
	resp := new(base.BaseResp)
	err = c.BindAndValidate(&req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusBadRequest, resp)
		return
	}

	err = logic.NewUser(data.Default()).DeleteUser(ctx, req.ID)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}

	resp.ErrCode = base.ErrCode_Success
	resp.ErrMsg = "success delete user"

	c.JSON(consts.StatusOK, resp)
}

// UpdateProfile .
// @router /api/admin/user/profile [POST]
func UpdateProfile(ctx context.Context, c *app.RequestContext) {
	var err error
	var req admin.ProfileReq
	resp := new(base.BaseResp)
	err = c.BindAndValidate(&req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusBadRequest, resp)
		return
	}

	var profileReq admin2.UpdateUserProfileReq
	err = copier.Copy(&profileReq, &req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}
	err = logic.NewUser(data.Default()).UpdateProfile(ctx, profileReq)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}

	resp.ErrCode = base.ErrCode_Success
	resp.ErrMsg = "success"
	c.JSON(consts.StatusOK, resp)
}

// UserProfile .
// @router /api/admin/user/profile [GET]
func UserProfile(ctx context.Context, c *app.RequestContext) {
	var err error
	var req base.Empty
	resp := new(admin.ProfileResp)
	err = c.BindAndValidate(&req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusBadRequest, resp)
		return
	}

	v, exist := c.Get("userID")
	if !exist || v == nil {
		c.JSON(consts.StatusUnauthorized, "Unauthorized")
		return
	}
	i, err := strconv.Atoi(v.(string))
	if err != nil {
		c.JSON(consts.StatusUnauthorized, "Unauthorized,"+err.Error())
		return
	}
	userID := uint64(i)
	// get user info
	user, err := logic.NewUser(data.Default()).UserInfo(ctx, userID)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}
	resp.Mobile = user.Mobile
	resp.Avatar = user.Avatar
	resp.Nickname = user.Nickname
	resp.Email = user.Email

	resp.ErrCode = base.ErrCode_Success
	resp.ErrMsg = "success"
	c.JSON(consts.StatusOK, resp)
}

// UpdateUserStatus .
// @router /api/admin/user/status [POST]
func UpdateUserStatus(ctx context.Context, c *app.RequestContext) {
	var err error
	var req base.StatusCodeReq
	resp := new(base.BaseResp)
	err = c.BindAndValidate(&req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusBadRequest, resp)
		return
	}

	err = logic.NewUser(data.Default()).UpdateUserStatus(ctx, req.ID, req.Status)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}

	resp.ErrCode = base.ErrCode_Success
	resp.ErrMsg = "success"
	c.JSON(consts.StatusOK, resp)
}
