package app

import (
	"bindbox-game/internal/code"
	"bindbox-game/internal/pkg/core"
	"bindbox-game/internal/pkg/validation"
	"net/http"
)

type requestShippingBatchRequest struct {
	InventoryIDs []int64 `json:"inventory_ids"`
	AddressID    *int64  `json:"address_id"`
}

type requestShippingBatchResponse struct {
	AddressID  int64            `json:"address_id"`
	BatchNo    string           `json:"batch_no"`
	SuccessIDs []int64          `json:"success_ids"`
	Skipped    []map[string]any `json:"skipped"`
	Failed     []map[string]any `json:"failed"`
}

// RequestShippingBatch 批量申请发货（使用默认地址或指定地址）
// @Summary 批量申请发货
// @Description 为多个资产申请发货，校验所有权与状态；幂等：已申请的资产跳过
// @Tags APP端.用户
// @Accept json
// @Produce json
// @Security LoginVerifyToken
// @Param user_id path integer true "用户ID"
// @Param RequestBody body requestShippingBatchRequest true "请求参数：资产ID列表与可选地址ID"
// @Success 200 {object} requestShippingBatchResponse
// @Failure 400 {object} code.Failure
// @Router /api/app/users/{user_id}/inventory/request-shipping-batch [post]
func (h *handler) RequestShippingBatch() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(requestShippingBatchRequest)
		rsp := new(requestShippingBatchResponse)
		if err := ctx.ShouldBindJSON(req); err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
			return
		}
		if len(req.InventoryIDs) == 0 || len(req.InventoryIDs) > 100 {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "invalid inventory_ids"))
			return
		}
		userID := int64(ctx.SessionUserInfo().Id)
		addrID, batchNo, success, skipped, failed, err := h.user.RequestShippings(ctx.RequestContext(), userID, req.InventoryIDs, req.AddressID)
		if err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, 10023, err.Error()))
			return
		}
		rsp.AddressID = addrID
		rsp.BatchNo = batchNo
		rsp.SuccessIDs = success
		for _, s := range skipped {
			rsp.Skipped = append(rsp.Skipped, map[string]any{"id": s.ID, "reason": s.Reason})
		}
		for _, f := range failed {
			rsp.Failed = append(rsp.Failed, map[string]any{"id": f.ID, "reason": f.Reason})
		}
		ctx.Payload(rsp)
	}
}
