package event

import (
	"kpt-pasture/http/middleware"
	"net/http"

	operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation"
	"gitee.com/xuyiping_admin/pkg/valid"

	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
	"gitee.com/xuyiping_admin/pkg/apierr"
	"gitee.com/xuyiping_admin/pkg/ginutil"
	"github.com/gin-gonic/gin"
)

func EnterEventList(c *gin.Context) {
	var req pasturePb.SearchEventRequest
	if err := ginutil.BindProto(c, &req); err != nil {
		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
		return
	}

	pagination := &pasturePb.PaginationModel{
		Page:       int32(c.GetInt(middleware.Page)),
		PageSize:   int32(c.GetInt(middleware.PageSize)),
		PageOffset: int32(c.GetInt(middleware.PageOffset)),
	}

	res, err := middleware.Dependency(c).StoreEventHub.OpsService.EnterList(c, &req, pagination)
	if err != nil {
		apierr.ClassifiedAbort(c, err)
		return
	}
	ginutil.JSONResp(c, res)
}

func EnterEventCreate(c *gin.Context) {
	var req pasturePb.EventEnterRequest
	if err := ginutil.BindProto(c, &req); err != nil {
		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
		return
	}

	if err := valid.ValidateStruct(&req,
		valid.Field(&req.EarNumber, valid.Required),
		valid.Field(&req.BirthAt, valid.Required),
		valid.Field(&req.Sex, valid.Required),
		valid.Field(&req.CowType, valid.Required),
		valid.Field(&req.CowKind, valid.Required),
		valid.Field(&req.CowSource, valid.Required),
		valid.Field(&req.Lact, valid.Required),
		valid.Field(&req.EnterAt, valid.Required),
		valid.Field(&req.CowSource, valid.Required),
		valid.Field(&req.OperationId, valid.Required),
	); err != nil {
		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
		return
	}

	if err := middleware.BackendOperation(c).OpsService.CreateEnter(c, &req); err != nil {
		apierr.ClassifiedAbort(c, err)
		return
	}

	ginutil.JSONResp(c, &operationPb.CommonOK{
		Code: http.StatusOK,
		Msg:  "ok",
		Data: &operationPb.Success{Success: true},
	})
}

func GroupTransferEventList(c *gin.Context) {
	var req pasturePb.SearchEventRequest
	if err := ginutil.BindProto(c, &req); err != nil {
		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
		return
	}

	pagination := &pasturePb.PaginationModel{
		Page:       int32(c.GetInt(middleware.Page)),
		PageSize:   int32(c.GetInt(middleware.PageSize)),
		PageOffset: int32(c.GetInt(middleware.PageOffset)),
	}

	res, err := middleware.Dependency(c).StoreEventHub.OpsService.GroupTransferList(c, &req, pagination)
	if err != nil {
		apierr.ClassifiedAbort(c, err)
		return
	}
	ginutil.JSONResp(c, res)
}

func GroupTransferEventCreate(c *gin.Context) {
	var req pasturePb.TransferGroupEventRequest
	if err := ginutil.BindProto(c, &req); err != nil {
		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
		return
	}

	if err := valid.ValidateStruct(&req,
		valid.Field(&req.Body, valid.Required, valid.Each(valid.By(func(value interface{}) error {
			s := value.(pasturePb.TransferGroupEventData)
			return valid.ValidateStruct(&s,
				valid.Field(&s.CowId, valid.Required),
				valid.Field(&s.TransferDate, valid.Required),
				valid.Field(&s.TransferInPenId, valid.Required),
				valid.Field(&s.TransferReasonId, valid.Required),
				valid.Field(&s.StaffMemberId, valid.Required),
			)
		}))),
	); err != nil {
		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
		return
	}

	if err := middleware.BackendOperation(c).OpsService.CreateGroupTransfer(c, &req); err != nil {
		apierr.ClassifiedAbort(c, err)
		return
	}
	ginutil.JSONResp(c, &operationPb.CommonOK{
		Code: http.StatusOK,
		Msg:  "ok",
		Data: &operationPb.Success{Success: true},
	})
}

func BodyScoreEventList(c *gin.Context) {
	var req pasturePb.SearchEventRequest
	if err := ginutil.BindProto(c, &req); err != nil {
		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
		return
	}

	pagination := &pasturePb.PaginationModel{
		Page:       int32(c.GetInt(middleware.Page)),
		PageSize:   int32(c.GetInt(middleware.PageSize)),
		PageOffset: int32(c.GetInt(middleware.PageOffset)),
	}

	res, err := middleware.Dependency(c).StoreEventHub.OpsService.BodyScoreList(c, &req, pagination)
	if err != nil {
		apierr.ClassifiedAbort(c, err)
		return
	}
	ginutil.JSONResp(c, res)
}

func BodyScoreEventCreate(c *gin.Context) {
	var req pasturePb.BodyScoreEventRequest
	if err := ginutil.BindProto(c, &req); err != nil {
		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
		return
	}

	if err := valid.ValidateStruct(&req,
		valid.Field(&req.CowId, valid.Required),
		valid.Field(&req.ScoreAt, valid.Required),
		valid.Field(&req.Score, valid.Required),
		valid.Field(&req.StaffMemberId, valid.Required),
	); err != nil {
		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
		return
	}

	if err := middleware.BackendOperation(c).OpsService.CreateBodyScore(c, &req); err != nil {
		apierr.ClassifiedAbort(c, err)
		return
	}

	ginutil.JSONResp(c, &operationPb.CommonOK{
		Code: http.StatusOK,
		Msg:  "ok",
		Data: &operationPb.Success{Success: true},
	})
}

func WeightEventList(c *gin.Context) {
	var req pasturePb.SearchEventRequest
	if err := ginutil.BindProto(c, &req); err != nil {
		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
		return
	}

	pagination := &pasturePb.PaginationModel{
		Page:       int32(c.GetInt(middleware.Page)),
		PageSize:   int32(c.GetInt(middleware.PageSize)),
		PageOffset: int32(c.GetInt(middleware.PageOffset)),
	}

	res, err := middleware.Dependency(c).StoreEventHub.OpsService.WeightList(c, &req, pagination)
	if err != nil {
		apierr.ClassifiedAbort(c, err)
		return
	}
	ginutil.JSONResp(c, res)
}

func WeightEventCreate(c *gin.Context) {
	var req pasturePb.EventWeight
	if err := ginutil.BindProto(c, &req); err != nil {
		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
		return
	}

	if err := valid.ValidateStruct(&req,
		valid.Field(&req.CowId, valid.Required),
		valid.Field(&req.WeightAt, valid.Required),
		valid.Field(&req.StaffMemberId, valid.Required),
		valid.Field(&req.WeightItems, valid.Required, valid.Each(valid.By(func(value interface{}) error {
			s := value.(pasturePb.WeightItem)
			return valid.ValidateStruct(&s,
				valid.Field(&s.Weight, valid.Required),
			)
		}))),
	); err != nil {
		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
		return
	}

	if err := middleware.BackendOperation(c).OpsService.WeightCreate(c, &req); err != nil {
		apierr.ClassifiedAbort(c, err)
		return
	}

	ginutil.JSONResp(c, &operationPb.CommonOK{
		Code: http.StatusOK,
		Msg:  "ok",
		Data: &operationPb.Success{Success: true},
	})
}