package app

import (
	"net/http"
	"strconv"

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

type listGuildsRequest struct {
	Name     string `form:"name"`
	IsOpen   int32  `form:"is_open"`
	Status   int32  `form:"status"`
	JoinMode int32  `form:"join_mode"`
	Page     int    `form:"page"`
	PageSize int    `form:"page_size"`
}

type guildItem struct {
	ID           int64  `json:"id"`
	Name         string `json:"name"`
	OwnerID      int64  `json:"owner_id"`
	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 listGuildsResponse struct {
	Page     int         `json:"page"`
	PageSize int         `json:"page_size"`
	Total    int64       `json:"total"`
	List     []guildItem `json:"list"`
}

// ListGuilds 工会列表
// @Summary 浏览工会列表
// @Description 获取工会列表，支持公开与状态过滤以及分页
// @Tags APP端.工会
// @Accept json
// @Produce json
// @Param name query string false "工会名称(模糊)"
// @Param is_open query int false "是否公开(1公开 2私有)"
// @Param status query int false "状态(1正常 2解散)"
// @Param join_mode query int false "加入方式(1审核 2自动 3消费流水)"
// @Param page query int true "页码" default(1)
// @Param page_size query int true "每页数量，最多100" default(20)
// @Success 200 {object} listGuildsResponse
// @Failure 400 {object} code.Failure
// @Router /api/app/guilds [get]
func (h *handler) ListGuilds() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(listGuildsRequest)
		res := new(listGuildsResponse)
		if err := ctx.ShouldBindForm(req); err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
			return
		}
		var isOpenPtr, statusPtr, joinModePtr *int32
		if req.IsOpen == 1 || req.IsOpen == 2 {
			isOpenPtr = &req.IsOpen
		}
		if req.Status == 1 || req.Status == 2 {
			statusPtr = &req.Status
		}
		if req.JoinMode == 1 || req.JoinMode == 2 || req.JoinMode == 3 {
			joinModePtr = &req.JoinMode
		}
		items, total, err := h.guild.ListGuilds(ctx.RequestContext(), struct {
			Name     string
			IsOpen   *int32
			Status   *int32
			JoinMode *int32
			Page     int
			PageSize int
		}{Name: req.Name, IsOpen: isOpenPtr, Status: statusPtr, JoinMode: joinModePtr, Page: req.Page, PageSize: req.PageSize})
		if err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ListGuildsError, err.Error()))
			return
		}
		res.Page = req.Page
		res.PageSize = req.PageSize
		res.Total = total
		res.List = make([]guildItem, len(items))
		for i, v := range items {
			res.List[i] = guildItem{ID: v.ID, Name: v.Name, OwnerID: v.OwnerID, Description: v.Description, JoinMode: v.JoinMode, ConsumeLimit: v.ConsumeLimit, AvatarURL: v.AvatarURL, IsOpen: v.IsOpen, Status: v.Status}
		}
		ctx.Payload(res)
	}
}

// GetGuildDetail 工会详情
// @Summary 查看工会详情
// @Description 查看指定工会详情
// @Tags APP端.工会
// @Accept json
// @Produce json
// @Param guild_id path integer true "工会ID"
// @Success 200 {object} model.Guild
// @Failure 400 {object} code.Failure
// @Router /api/app/guilds/{guild_id} [get]
func (h *handler) GetGuildDetail() core.HandlerFunc {
	return func(ctx core.Context) {
		id, err := strconv.ParseInt(ctx.Param("guild_id"), 10, 64)
		if err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "未传递工会ID"))
			return
		}
		item, err := h.guild.GetGuild(ctx.RequestContext(), id)
		if err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.GetGuildError, err.Error()))
			return
		}
		ctx.Payload(item)
	}
}
