package backend

import (
	"context"
	"kpt-pasture/model"

	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
	"gitee.com/xuyiping_admin/pkg/xerr"
)

// EventCheckBatchModel 批量事件
type EventCheckBatchModel struct {
	CowList       []*model.Cow
	CurrentUser   *model.SystemUser
	OperationUser *model.SystemUser
}

// PregnantCheckBatchModel 批量孕检
type PregnantCheckBatchModel struct {
	EventPregnancyCheck *model.EventPregnantCheck
	Cow                 *model.Cow
	OperationUser       *model.SystemUser
	PregnantCheckAt     int32                              // 孕检日期
	PregnantCheckResult pasturePb.PregnantCheckResult_Kind // 孕检结果
	PregnantCheckMethod pasturePb.PregnantCheckMethod_Kind // 孕检方式
	Remarks             string
}

// MatingTimes 更新配次
type MatingTimes struct {
	Mt            int32
	CowId         int64
	EventMatingId int64
}

func (s *StoreEntry) MatingCreateCheck(ctx context.Context, req *pasturePb.EventMating) (*EventCheckBatchModel, error) {
	if len(req.CowIds) <= 0 {
		return nil, xerr.Custom("请选择相关牛只")
	}

	cowList, err := s.ParseCowIds(ctx, req.CowIds)
	if err != nil {
		return nil, xerr.WithStack(err)
	}

	operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
	if err != nil {
		return nil, xerr.WithStack(err)
	}

	currentUser, err := s.GetCurrentSystemUser(ctx)
	if err != nil {
		return nil, xerr.Customf("获取当前用户失败: %s", err.Error())
	}

	for _, cow := range cowList {
		if cow.Sex != pasturePb.Genders_Female {
			return nil, xerr.Customf("牛只: %d,不是母牛", cow.Id)
		}
		if int64(req.MatingAt) < cow.LastMatingAt {
			return nil, xerr.Customf("牛只: %d,最近一次配种时间: %d,不能小于本次配种时间: %d", cow.Id, cow.LastMatingAt, req.MatingAt)
		}

		if int64(req.MatingAt) < cow.LastPregnantCheckAt {
			return nil, xerr.Customf("牛只: %d,最近一次孕检时间: %d,不能小于本次配种时间: %d", cow.Id, cow.LastPregnantCheckAt, req.MatingAt)
		}

		if int64(req.MatingAt) < cow.LastAbortionAt {
			return nil, xerr.Customf("牛只: %d,最近一次流产时间: %d,不能小于本次配种时间: %d", cow.Id, cow.LastAbortionAt, req.MatingAt)
		}

		if int64(req.MatingAt) < cow.BirthAt {
			return nil, xerr.Customf("牛只: %d,出生时间: %d,不能小于本次配种时间: %d", cow.Id, cow.BirthAt, req.MatingAt)
		}

		if cow.BreedStatus == pasturePb.BreedStatus_Pregnant || cow.BreedStatus == pasturePb.BreedStatus_No_Mating {
			return nil, xerr.Customf("牛只: %d,当前状态为: %s,不能进行配种", cow.Id, cow.BreedStatus.String())
		}
	}
	return &EventCheckBatchModel{
		CowList:       cowList,
		CurrentUser:   currentUser,
		OperationUser: operationUser,
	}, nil
}

func (s *StoreEntry) PregnantCheckCreateCheck(ctx context.Context, req *pasturePb.EventPregnantCheckBatch) ([]*PregnantCheckBatchModel, error) {
	if len(req.Item) <= 0 {
		return nil, xerr.Custom("请选择相关牛只")
	}

	pregnantCheckBatchModelList := make([]*PregnantCheckBatchModel, 0)
	for _, pregnantCheckData := range req.Item {
		operationUser, err := s.GetSystemUserById(ctx, int64(pregnantCheckData.OperationId))
		if err != nil {
			return nil, xerr.WithStack(err)
		}
		cow, err := s.GetCowInfoByCowId(ctx, int64(pregnantCheckData.CowId))
		if err != nil {
			return nil, xerr.WithStack(err)
		}

		// 过滤掉没有配种状态的牛只
		if cow.BreedStatus != pasturePb.BreedStatus_Breeding {
			return nil, xerr.Customf("牛只: %d,当前状态为: %s,不能进行孕检", cow.Id, cow.BreedStatus.String())
		}

		pregnantCheckBatchModelList = append(pregnantCheckBatchModelList, &PregnantCheckBatchModel{
			Cow:                 cow,
			OperationUser:       operationUser,
			PregnantCheckAt:     pregnantCheckData.PregnantCheckAt,
			PregnantCheckMethod: pregnantCheckData.PregnantCheckMethod,
			PregnantCheckResult: pregnantCheckData.PregnantCheckResult,
			Remarks:             pregnantCheckData.Remarks,
		})
	}

	return pregnantCheckBatchModelList, nil
}