package group

import (
	"net/http"
	"tmr-watch/models"
	"tmr-watch/pkg/app"
	"tmr-watch/pkg/e"
	"tmr-watch/service/group"

	"github.com/gin-gonic/gin"
)

const (
	FeedCategoryDeleteKey = "feed_delete"
	CowCategoryDeleteKey  = "cow_delete"
)

func Handle404(c *gin.Context) {
	c.String(http.StatusNotFound, "404 NotFound")
}

// DistributeFeedFormula 饲料配方下发
func DistributeFeedFormula(c *gin.Context) {
	appG := app.Gin{C: c}
	var req models.PastureBodyRequest
	if err := c.BindJSON(&req); err != nil {
		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
		return
	}

	if len(req.Body) <= 0 {
		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
		return
	}

	if err := group.DistributeFeedFormulaService(&req); err != nil {
		appG.Response(http.StatusBadRequest, e.ERROR_ADD_FAIL, nil)
		return
	}

	appG.Response(http.StatusOK, e.SUCCESS, map[string]bool{
		"success": true,
	})
}

func FeedFormulaIsModify(c *gin.Context) {
	appG := app.Gin{C: c}
	var req models.PastureFeedFormulaIsModifyRequest
	if err := c.BindJSON(&req); err != nil {
		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
		return
	}

	if req.PastureId <= 0 {
		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
		return
	}

	if err := group.FeedFormulaIsModifyService(&req); err != nil {
		appG.Response(http.StatusBadRequest, e.ERROR_ADD_FAIL, nil)
		return
	}

	appG.Response(http.StatusOK, e.SUCCESS, map[string]bool{
		"success": true,
	})
}

// AnalysisAccuracy 准确性分析
func AnalysisAccuracy(c *gin.Context) {
	appG := app.Gin{C: c}
	var req models.AnalysisAccuracyRequest
	if err := c.BindJSON(&req); err != nil {
		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
		return
	}
	if res, err := group.AnalysisAccuracyService(&req); err != nil {
		appG.Response(http.StatusBadRequest, e.ERROR_GET_S_FAIL, map[string]interface{}{
			"error": err,
		})
	} else {
		appG.Response(http.StatusOK, e.SUCCESS, res)
	}
}

// ProcessAnalysis 过程分析
func ProcessAnalysis(c *gin.Context) {
	appG := app.Gin{C: c}
	var req models.AnalysisAccuracyRequest
	if err := c.BindJSON(&req); err != nil {
		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
		return
	}
	if res, err := group.ProcessAnalysisService(&req); err != nil {
		appG.Response(http.StatusBadRequest, e.ERROR_GET_S_FAIL, map[string]interface{}{
			"error": err,
		})
	} else {
		appG.Response(http.StatusOK, e.SUCCESS, res)
	}
}

// SprinkleStatistics 撒料统计
func SprinkleStatistics(c *gin.Context) {
	appG := app.Gin{C: c}
	var req models.SprinkleStatisticsRequest
	if err := c.BindJSON(&req); err != nil {
		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
		return
	}
	if res, err := group.SprinkleStatisticsService(&req); err != nil {
		appG.Response(http.StatusBadRequest, e.ERROR_GET_S_FAIL, map[string]interface{}{
			"error": err,
		})
	} else {
		appG.Response(http.StatusOK, e.SUCCESS, res)
	}
}

// DistributeAccount 账号下发
func DistributeAccount(c *gin.Context) {
	appG := app.Gin{C: c}
	var req models.AccountDistributionRequest
	if err := c.BindJSON(&req); err != nil {
		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
		return
	}
	if err := group.AccountDistributionService(&req); err != nil {
		appG.Response(http.StatusBadRequest, e.ERROR_GET_S_FAIL, map[string]interface{}{
			"error": err,
		})
	} else {
		appG.Response(http.StatusOK, e.SUCCESS, map[string]bool{
			"success": true,
		})
	}
}

// ForageCategoryDistribute 饲料分类下发
func ForageCategoryDistribute(c *gin.Context) {
	appG := app.Gin{C: c}
	var req models.CategoryRequest
	if err := c.BindJSON(&req); err != nil {
		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
		return
	}
	if err := group.ForageCategoryDistributeService(&req); err != nil {
		appG.Response(http.StatusBadRequest, e.ERROR_GET_S_FAIL, map[string]interface{}{
			"error": err,
		})
	} else {
		appG.Response(http.StatusOK, e.SUCCESS, map[string]bool{
			"success": true,
		})
	}
}

func ForageCategoryDelete(c *gin.Context) {
	appG := app.Gin{C: c}
	var req models.CategoryDeleteRequest
	if err := c.BindJSON(&req); err != nil {
		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
		return
	}
	if err := group.CategoryDeleteService(FeedCategoryDeleteKey, &req); err != nil {
		appG.Response(http.StatusBadRequest, e.ERROR_GET_S_FAIL, map[string]interface{}{
			"error": err,
		})
	} else {
		appG.Response(http.StatusOK, e.SUCCESS, map[string]bool{
			"success": true,
		})
	}
}

func CowCategoryDistribute(c *gin.Context) {
	appG := app.Gin{C: c}
	var req models.CategoryRequest
	if err := c.BindJSON(&req); err != nil {
		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
		return
	}
	if err := group.CattleCategoryDistributeService(&req); err != nil {
		appG.Response(http.StatusBadRequest, e.ERROR_GET_S_FAIL, map[string]interface{}{
			"error": err,
		})
	} else {
		appG.Response(http.StatusOK, e.SUCCESS, map[string]bool{
			"success": true,
		})
	}
}

func CowCategoryDelete(c *gin.Context) {
	appG := app.Gin{C: c}
	var req models.CategoryDeleteRequest
	if err := c.BindJSON(&req); err != nil {
		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
		return
	}
	if err := group.CategoryDeleteService(CowCategoryDeleteKey, &req); err != nil {
		appG.Response(http.StatusBadRequest, e.ERROR_GET_S_FAIL, map[string]interface{}{
			"error": err,
		})
	} else {
		appG.Response(http.StatusOK, e.SUCCESS, map[string]bool{
			"success": true,
		})
	}
}