package app

import (
    "net/http"

    "bindbox-game/internal/code"
    "bindbox-game/internal/pkg/core"
    "bindbox-game/internal/pkg/validation"
    "bindbox-game/internal/repository/mysql/model"
)

type listAddressesRequest struct {
    Page     int `form:"page"`
    PageSize int `form:"page_size"`
}

type listAddressesResponse struct {
    Page     int                    `json:"page"`
    PageSize int                    `json:"page_size"`
    Total    int64                  `json:"total"`
    List     []*model.UserAddresses `json:"list"`
}

// ListUserAddresses 获取用户地址列表
// @Summary 获取用户地址列表
// @Description 分页获取当前登录用户的收货地址列表，默认地址优先
// @Tags APP端.用户
// @Accept json
// @Produce json
// @Param user_id path integer true "用户ID"
// @Security LoginVerifyToken
// @Param page query integer false "页码，默认1"
// @Param page_size query integer false "每页条数，默认20，最多100"
// @Success 200 {object} listAddressesResponse
// @Failure 400 {object} code.Failure "参数错误"
// @Failure 401 {object} code.Failure "未授权"
// @Failure 500 {object} code.Failure "服务器内部错误"
// @Router /api/app/users/{user_id}/addresses [get]
func (h *handler) ListUserAddresses() core.HandlerFunc {
    return func(ctx core.Context) {
        req := new(listAddressesRequest)
        rsp := new(listAddressesResponse)
        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.ListAddresses(ctx.RequestContext(), userID, req.Page, req.PageSize)
        if err != nil {
            ctx.AbortWithError(core.Error(http.StatusBadRequest, 10012, err.Error()))
            return
        }
        rsp.Page = req.Page
        rsp.PageSize = req.PageSize
        rsp.Total = total
        rsp.List = items
        ctx.Payload(rsp)
    }
}