package management

import (
	"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
	"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"

	"github.com/labstack/echo/v4"
)

// SellPigs godoc
// @Summary      处理卖猪的业务逻辑
// @Description  记录猪批次中的猪只出售事件
// @Tags         猪群管理
// @Security     BearerAuth
// @Accept       json
// @Produce      json
// @Param        id path int true "猪批次ID"
// @Param        body body dto.SellPigsRequest true "卖猪请求信息"
// @Success      200 {object} controller.Response "卖猪成功"
// @Router       /api/v1/pig-batches/sell-pigs/{id} [post]
func (c *PigBatchController) SellPigs(ctx echo.Context) error {
	reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "SellPigs")
	const action = "卖猪"
	var req dto.SellPigsRequest

	return handleAPIRequest(
		reqCtx, c, ctx, action, &req,
		func(ctx echo.Context, operatorID uint32, primaryID uint32, req *dto.SellPigsRequest) error {
			return c.service.SellPigs(reqCtx, primaryID, req.PenID, req.Quantity, req.UnitPrice, req.TotalPrice, req.TraderName, req.TradeDate, req.Remarks, operatorID)
		},
		"卖猪成功",
		nil, // 默认从 ":id" 路径参数提取ID
	)
}

// BuyPigs godoc
// @Summary      处理买猪的业务逻辑
// @Description  记录猪批次中的猪只购买事件
// @Tags         猪群管理
// @Security     BearerAuth
// @Accept       json
// @Produce      json
// @Param        id path int true "猪批次ID"
// @Param        body body dto.BuyPigsRequest true "买猪请求信息"
// @Success      200 {object} controller.Response "买猪成功"
// @Router       /api/v1/pig-batches/buy-pigs/{id} [post]
func (c *PigBatchController) BuyPigs(ctx echo.Context) error {
	reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "BuyPigs")
	const action = "买猪"
	var req dto.BuyPigsRequest

	return handleAPIRequest(
		reqCtx, c, ctx, action, &req,
		func(ctx echo.Context, operatorID uint32, primaryID uint32, req *dto.BuyPigsRequest) error {
			return c.service.BuyPigs(reqCtx, primaryID, req.PenID, req.Quantity, req.UnitPrice, req.TotalPrice, req.TraderName, req.TradeDate, req.Remarks, operatorID)
		},
		"买猪成功",
		nil, // 默认从 ":id" 路径参数提取ID
	)
}
