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"
)

// RecordSickPigs godoc
// @Summary      记录新增病猪事件
// @Description  记录猪批次中新增病猪的数量、治疗地点和发生时间
// @Tags         猪群管理
// @Security     BearerAuth
// @Accept       json
// @Produce      json
// @Param        id path int true "猪批次ID"
// @Param        body body dto.RecordSickPigsRequest true "记录病猪请求信息"
// @Success      200 {object} controller.Response "记录成功"
// @Router       /api/v1/pig-batches/record-sick-pigs/{id} [post]
func (c *PigBatchController) RecordSickPigs(ctx echo.Context) error {
	reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "RecordSickPigs")

	const action = "记录新增病猪事件"
	var req dto.RecordSickPigsRequest

	return handleAPIRequest(
		reqCtx, c, ctx, action, &req,
		func(ctx echo.Context, operatorID uint32, primaryID uint32, req *dto.RecordSickPigsRequest) error {
			return c.service.RecordSickPigs(reqCtx, operatorID, primaryID, req.PenID, req.Quantity, req.TreatmentLocation, req.HappenedAt, req.Remarks)
		},
		"记录成功",
		nil, // 默认从 ":id" 路径参数提取ID
	)
}

// RecordSickPigRecovery godoc
// @Summary      记录病猪康复事件
// @Description  记录猪批次中病猪康复的数量、治疗地点和发生时间
// @Tags         猪群管理
// @Security     BearerAuth
// @Accept       json
// @Produce      json
// @Param        id path int true "猪批次ID"
// @Param        body body dto.RecordSickPigRecoveryRequest true "记录病猪康复请求信息"
// @Success      200 {object} controller.Response "记录成功"
// @Router       /api/v1/pig-batches/record-sick-pig-recovery/{id} [post]
func (c *PigBatchController) RecordSickPigRecovery(ctx echo.Context) error {
	reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "RecordSickPigRecovery")

	const action = "记录病猪康复事件"
	var req dto.RecordSickPigRecoveryRequest

	return handleAPIRequest(
		reqCtx, c, ctx, action, &req,
		func(ctx echo.Context, operatorID uint32, primaryID uint32, req *dto.RecordSickPigRecoveryRequest) error {
			return c.service.RecordSickPigRecovery(reqCtx, operatorID, primaryID, req.PenID, req.Quantity, req.TreatmentLocation, req.HappenedAt, req.Remarks)
		},
		"记录成功",
		nil, // 默认从 ":id" 路径参数提取ID
	)
}

// RecordSickPigDeath godoc
// @Summary      记录病猪死亡事件
// @Description  记录猪批次中病猪死亡的数量、治疗地点和发生时间
// @Tags         猪群管理
// @Security     BearerAuth
// @Accept       json
// @Produce      json
// @Param        id path int true "猪批次ID"
// @Param        body body dto.RecordSickPigDeathRequest true "记录病猪死亡请求信息"
// @Success      200 {object} controller.Response "记录成功"
// @Router       /api/v1/pig-batches/record-sick-pig-death/{id} [post]
func (c *PigBatchController) RecordSickPigDeath(ctx echo.Context) error {
	reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "RecordSickPigDeath")

	const action = "记录病猪死亡事件"
	var req dto.RecordSickPigDeathRequest

	return handleAPIRequest(
		reqCtx, c, ctx, action, &req,
		func(ctx echo.Context, operatorID uint32, primaryID uint32, req *dto.RecordSickPigDeathRequest) error {
			return c.service.RecordSickPigDeath(reqCtx, operatorID, primaryID, req.PenID, req.Quantity, req.TreatmentLocation, req.HappenedAt, req.Remarks)
		},
		"记录成功",
		nil, // 默认从 ":id" 路径参数提取ID
	)
}

// RecordSickPigCull godoc
// @Summary      记录病猪淘汰事件
// @Description  记录猪批次中病猪淘汰的数量、治疗地点和发生时间
// @Tags         猪群管理
// @Security     BearerAuth
// @Accept       json
// @Produce      json
// @Param        id path int true "猪批次ID"
// @Param        body body dto.RecordSickPigCullRequest true "记录病猪淘汰请求信息"
// @Success      200 {object} controller.Response "记录成功"
// @Router       /api/v1/pig-batches/record-sick-pig-cull/{id} [post]
func (c *PigBatchController) RecordSickPigCull(ctx echo.Context) error {
	reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "RecordSickPigCull")

	const action = "记录病猪淘汰事件"
	var req dto.RecordSickPigCullRequest

	return handleAPIRequest(
		reqCtx, c, ctx, action, &req,
		func(ctx echo.Context, operatorID uint32, primaryID uint32, req *dto.RecordSickPigCullRequest) error {
			return c.service.RecordSickPigCull(reqCtx, operatorID, primaryID, req.PenID, req.Quantity, req.TreatmentLocation, req.HappenedAt, req.Remarks)
		},
		"记录成功",
		nil, // 默认从 ":id" 路径参数提取ID
	)
}

// RecordDeath godoc
// @Summary      记录正常猪只死亡事件
// @Description  记录猪批次中正常猪只死亡的数量和发生时间
// @Tags         猪群管理
// @Security     BearerAuth
// @Accept       json
// @Produce      json
// @Param        id path int true "猪批次ID"
// @Param        body body dto.RecordDeathRequest true "记录正常猪只死亡请求信息"
// @Success      200 {object} controller.Response "记录成功"
// @Router       /api/v1/pig-batches/record-death/{id} [post]
func (c *PigBatchController) RecordDeath(ctx echo.Context) error {
	reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "RecordDeath")

	const action = "记录正常猪只死亡事件"
	var req dto.RecordDeathRequest

	return handleAPIRequest(
		reqCtx, c, ctx, action, &req,
		func(ctx echo.Context, operatorID uint32, primaryID uint32, req *dto.RecordDeathRequest) error {
			return c.service.RecordDeath(reqCtx, operatorID, primaryID, req.PenID, req.Quantity, req.HappenedAt, req.Remarks)
		},
		"记录成功",
		nil, // 默认从 ":id" 路径参数提取ID
	)
}

// RecordCull godoc
// @Summary      记录正常猪只淘汰事件
// @Description  记录猪批次中正常猪只淘汰的数量和发生时间
// @Tags         猪群管理
// @Security     BearerAuth
// @Accept       json
// @Produce      json
// @Param        id path int true "猪批次ID"
// @Param        body body dto.RecordCullRequest true "记录正常猪只淘汰请求信息"
// @Success      200 {object} controller.Response "记录成功"
// @Router       /api/v1/pig-batches/record-cull/{id} [post]
func (c *PigBatchController) RecordCull(ctx echo.Context) error {
	reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "RecordCull")

	const action = "记录正常猪只淘汰事件"
	var req dto.RecordCullRequest

	return handleAPIRequest(
		reqCtx, c, ctx, action, &req,
		func(ctx echo.Context, operatorID uint32, primaryID uint32, req *dto.RecordCullRequest) error {
			return c.service.RecordCull(reqCtx, operatorID, primaryID, req.PenID, req.Quantity, req.HappenedAt, req.Remarks)
		},
		"记录成功",
		nil, // 默认从 ":id" 路径参数提取ID
	)
}
