package user

import (
	"fmt"
	"mini_server/internal/dao"
	"mini_server/internal/server/help"
	"mini_server/internal/server/httpreply"
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
)

/*
func ChangePersona(c *gin.Context) {
	reply := httpreply.NewDefaultReplyData()
	defer httpreply.Reply(c, reply)

	var param QueryURI
	if err := c.ShouldBindUri(&param); err != nil {
		reply.Status = http.StatusInternalServerError
		reply.Message = fmt.Sprintf("invalid uri, err:%v", err)
		return
	}

	persona := make(map[string]interface{})
	if err := c.ShouldBindJSON(&persona); err != nil {
		reply.Status = http.StatusInternalServerError
		reply.Message = fmt.Sprintf("invalid json,err:%v", err)
		return
	}

	if err := dao.User_Change_Persona(param.UserId, persona); err != nil {
		logrus.Errorf("dao.User_Change_Persona失败,userId:%v,persona:%v,err:%v", param.UserId, persona, err)
		reply.Status = http.StatusInternalServerError
		reply.Message = "更新个性化信息失败"
		return
	}
}

*/

type ChangeUsernameRequest struct {
	Username string `json:"username" binding:"min=1"` // 修改的用户名
}

func ChangeUsername(c *gin.Context) {
	reply := httpreply.NewDefaultReplyData()
	defer httpreply.Reply(c, reply)

	//解析请求参数
	var request ChangeUsernameRequest
	if err := c.ShouldBindJSON(&request); err != nil {
		reply.Status = http.StatusBadRequest
		reply.Message = fmt.Sprintf("invalid json data, err:%v", err)
		return
	}

	userId, ok := help.GetUserId(c)
	if !ok {
		reply.Status = http.StatusNotFound
		reply.Message = "无法获取用户信息"
		return
	}

	logrus.Tracef("修改用户名，userId:%d,请求参数:%v", userId, request)

	if err := dao.User_Change_Username(userId, request.Username); err != nil {
		logrus.Errorf("更新用户名称失败，userId:%d,err:%v", userId, err)
		reply.Status = http.StatusInternalServerError
		reply.Message = "更新用户名称失败"
		return
	}

	full_lastest_userinfo(reply, userId)
}
