package app

import (
	"net/http"
	"strconv"

	"bindbox-game/internal/code"
	"bindbox-game/internal/pkg/core"
	"bindbox-game/internal/pkg/validation"
)

type joinGuildRequest struct {
	UserID int64 `json:"user_id" binding:"required"`
}
type simpleMessageResponse struct {
	Message string `json:"message"`
}

// JoinGuild 加入工会
// @Summary 加入工会
// @Description 用户加入指定工会
// @Tags APP端.工会
// @Accept json
// @Produce json
// @Param guild_id path integer true "工会ID"
// @Param RequestBody body joinGuildRequest true "请求参数"
// @Success 200 {object} simpleMessageResponse
// @Failure 400 {object} code.Failure
// @Router /api/app/guilds/{guild_id}/members [post]
func (h *handler) JoinGuild() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(joinGuildRequest)
		res := new(simpleMessageResponse)
		if err := ctx.ShouldBindJSON(req); err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
			return
		}
		guildID, err := strconv.ParseInt(ctx.Param("guild_id"), 10, 64)
		if err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "未传递工会ID"))
			return
		}
		if err := h.guild.JoinGuild(ctx.RequestContext(), guildID, struct{ UserID int64 }{UserID: req.UserID}); err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.JoinGuildError, err.Error()))
			return
		}
		res.Message = "操作成功"
		ctx.Payload(res)
	}
}

// LeaveGuild 离开工会
// @Summary 离开工会
// @Description 用户离开指定工会
// @Tags APP端.工会
// @Accept json
// @Produce json
// @Param guild_id path integer true "工会ID"
// @Param user_id path integer true "用户ID"
// @Success 200 {object} simpleMessageResponse
// @Failure 400 {object} code.Failure
// @Router /api/app/guilds/{guild_id}/members/{user_id} [delete]
func (h *handler) LeaveGuild() core.HandlerFunc {
	return func(ctx core.Context) {
		res := new(simpleMessageResponse)
		guildID, err := strconv.ParseInt(ctx.Param("guild_id"), 10, 64)
		if err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "未传递工会ID"))
			return
		}
		userID, err := strconv.ParseInt(ctx.Param("user_id"), 10, 64)
		if err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "未传递用户ID"))
			return
		}
		if err := h.guild.LeaveGuild(ctx.RequestContext(), guildID, userID); err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.LeaveGuildError, err.Error()))
			return
		}
		res.Message = "操作成功"
		ctx.Payload(res)
	}
}

type listMembersRequest struct {
	Page     int `form:"page"`
	PageSize int `form:"page_size"`
}
type memberItem struct {
	ID        int64  `json:"id"`
	UserID    int64  `json:"user_id"`
	Role      string `json:"role"`
	StartTime string `json:"start_time"`
}
type listMembersResponse struct {
	Page     int          `json:"page"`
	PageSize int          `json:"page_size"`
	Total    int64        `json:"total"`
	List     []memberItem `json:"list"`
}

// ListGuildMembers 工会成员列表
// @Summary 查看工会成员
// @Description 查看指定工会的成员列表
// @Tags APP端.工会
// @Accept json
// @Produce json
// @Param guild_id path integer true "工会ID"
// @Param page query int true "页码" default(1)
// @Param page_size query int true "每页数量，最多100" default(20)
// @Success 200 {object} listMembersResponse
// @Failure 400 {object} code.Failure
// @Router /api/app/guilds/{guild_id}/members [get]
func (h *handler) ListGuildMembers() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(listMembersRequest)
		res := new(listMembersResponse)
		if err := ctx.ShouldBindForm(req); err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
			return
		}
		guildID, err := strconv.ParseInt(ctx.Param("guild_id"), 10, 64)
		if err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "未传递工会ID"))
			return
		}
		items, total, err := h.guild.ListMembers(ctx.RequestContext(), guildID, req.Page, req.PageSize)
		if err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ListGuildMembersError, err.Error()))
			return
		}
		res.Page = req.Page
		res.PageSize = req.PageSize
		res.Total = total
		res.List = make([]memberItem, len(items))
		for i, v := range items {
			res.List[i] = memberItem{ID: v.ID, UserID: v.UserID, Role: v.Role, StartTime: v.StartTime.Format("2006-01-02 15:04:05")}
		}
		ctx.Payload(res)
	}
}
