package admin

import (
	"net/http"
	"strconv"

	"bindbox-game/internal/code"
	"bindbox-game/internal/pkg/core"
	"bindbox-game/internal/pkg/validation"
	guildsvc "bindbox-game/internal/service/guild"
)

type modifyGuildRequest struct {
	Name         string `json:"name"`
	Description  string `json:"description"`
	JoinMode     int32  `json:"join_mode"`
	ConsumeLimit int64  `json:"consume_limit"`
	AvatarURL    string `json:"avatar_url"`
	IsOpen       int32  `json:"is_open"`
	Status       int32  `json:"status"`
}

type modifyGuildResponse struct {
	Message string `json:"message"`
}

// ModifyGuild 修改工会
// @Summary 修改工会
// @Description 修改指定工会信息
// @Tags 管理端.工会
// @Accept json
// @Produce json
// @Param guild_id path integer true "工会ID"
// @Param RequestBody body modifyGuildRequest true "请求参数"
// @Success 200 {object} modifyGuildResponse
// @Failure 400 {object} code.Failure
// @Router /api/admin/guilds/{guild_id} [put]
// @Security LoginVerifyToken
func (h *handler) ModifyGuild() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(modifyGuildRequest)
		res := new(modifyGuildResponse)
		if err := ctx.ShouldBindJSON(req); err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
			return
		}
		if ctx.SessionUserInfo().IsSuper != 1 {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ModifyGuildError, "禁止操作"))
			return
		}
		id, 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.ModifyGuild(ctx.RequestContext(), id, guildsvc.ModifyGuildInput{
			Name: req.Name, Description: req.Description, JoinMode: req.JoinMode, ConsumeLimit: req.ConsumeLimit, AvatarURL: req.AvatarURL, IsOpen: req.IsOpen, Status: req.Status,
		}); err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ModifyGuildError, err.Error()))
			return
		}
		res.Message = "操作成功"
		ctx.Payload(res)
	}
}
