package role

import (
	"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"
)

type QueryListReply struct {
	RoleList []*RoleInfo `json:"role_list"`
}

// 角色列表查询接口，根据用户ID查询
func QueryList(c *gin.Context) {
	reply := httpreply.NewDefaultReplyData()
	defer httpreply.Reply(c, reply)

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

	// 1. 需要去表user_character中查询关联的角色ID
	list, err := dao.UserCharacter_QueryList(userId)
	if err != nil {
		logrus.Errorf("查询用户关联角色列表失败,err:%v", err)
		reply.Status = http.StatusInternalServerError
		reply.Message = "查询用户关联角色列表失败"
		return
	}

	// 2. 根据角色ID列表，查询角色详情
	roleIds := make([]int32, len(list))
	for i, data := range list {
		roleIds[i] = data.CharacterId
	}

	roleList, err := dao.Character_QueryList(roleIds)
	if err != nil {
		logrus.Errorf("查询角色列表失败,userId:%v,roleIds:%v,err:%v", userId, roleIds, err)
		reply.Status = http.StatusInternalServerError
		reply.Message = "查询角色列表失败"
		return
	}

	replyroleList := make([]*RoleInfo, len(list))
	for i, data := range roleList {
		var detail RoleInfo
		Copy_mysql_data(data, &detail)
		replyroleList[i] = &detail
	}
	reply.Data = roleList
}
