package admin

import (
    "net/http"

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

type batchUpdateProductsRequest struct {
    IDs   []int64 `json:"ids" binding:"required"`
    Stock *int64  `json:"stock"`
    Status *int32 `json:"status"`
}

type batchUpdateProductsResponse struct {
    UpdatedCount int64  `json:"updated_count"`
    Message      string `json:"message"`
}

// BatchUpdateProducts 批量更新商品
// @Summary 批量更新商品（库存/上下架）
// @Tags 管理端.商品
// @Accept json
// @Produce json
// @Param RequestBody body batchUpdateProductsRequest true "请求参数"
// @Success 200 {object} batchUpdateProductsResponse
// @Failure 400 {object} code.Failure
// @Router /api/admin/products/batch [put]
// @Security LoginVerifyToken
func (h *handler) BatchUpdateProducts() core.HandlerFunc {
    return func(ctx core.Context) {
        req := new(batchUpdateProductsRequest)
        res := new(batchUpdateProductsResponse)
        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.CreateAdminError, "禁止操作"))
            return
        }
        if len(req.IDs) == 0 || (req.Stock == nil && req.Status == nil) {
            ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "参数错误"))
            return
        }
        if len(req.IDs) > 1000 {
            ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "最多支持1000个ID"))
            return
        }

        updated, err := h.product.BatchUpdate(ctx.RequestContext(), req.IDs, req.Stock, req.Status)
        if err != nil {
            ctx.AbortWithError(core.Error(http.StatusBadRequest, code.CreateAdminError, err.Error()))
            return
        }
        res.UpdatedCount = updated
        res.Message = "操作成功"
        ctx.Payload(res)
    }
}