package app

import (
    "net/http"

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

type listInvitesRequest struct {
	Page     int `form:"page"`
	PageSize int `form:"page_size"`
}
type inviteUserItem struct {
	ID         int64  `json:"id"`
	Nickname   string `json:"nickname"`
	Avatar     string `json:"avatar"`
	InviteCode string `json:"invite_code"`
}
type listInvitesResponse struct {
	Page     int               `json:"page"`
	PageSize int               `json:"page_size"`
	Total    int64             `json:"total"`
	List     []*inviteUserItem `json:"list"`
}

// ListUserInvites 查看用户邀请记录
// @Summary 查看用户邀请记录
// @Description 查看被该用户邀请的用户列表
// @Tags APP端.用户
// @Accept json
// @Produce json
    // @Param user_id path integer true "用户ID"
    // @Security LoginVerifyToken
// @Param page query int true "页码" default(1)
// @Param page_size query int true "每页数量，最多100" default(20)
// @Success 200 {object} listInvitesResponse
// @Failure 400 {object} code.Failure
// @Router /api/app/users/{user_id}/invites [get]
func (h *handler) ListUserInvites() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(listInvitesRequest)
		rsp := new(listInvitesResponse)
		if err := ctx.ShouldBindForm(req); err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
			return
		}
        userID := int64(ctx.SessionUserInfo().Id)
        items, total, err := h.user.ListInvites(ctx.RequestContext(), userID, req.Page, req.PageSize)
		if err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, 10007, err.Error()))
			return
		}
		rsp.Page = req.Page
		rsp.PageSize = req.PageSize
		rsp.Total = total
		var list = make([]*inviteUserItem, 0, len(items))
		for _, it := range items {
			list = append(list, &inviteUserItem{ID: it.ID, Nickname: it.Nickname, Avatar: it.Avatar, InviteCode: it.InviteCode})
		}
		rsp.List = list
		ctx.Payload(rsp)
	}
}
