package model

import (
	"fmt"
	"kpt-pasture/store/kptstore"

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

type NeckRingEstrus struct {
	Id               int64                           `json:"id"`
	PastureId        int64                           `json:"pastureId"`
	CowId            int64                           `json:"cowId"`
	NeckRingNumber   string                          `json:"neckRingNumber"`
	EarNumber        string                          `json:"earNumber"`
	Lact             int32                           `json:"lact"`
	ExposeEstrusType pasturePb.ExposeEstrusType_Kind `json:"exposeEstrusType"`
	EstrusStartDate  string                          `json:"estrusStartDate"`
	ActiveDate       string                          `json:"activeDate"`
	LastEstrusDate   string                          `json:"lastEstrusDate"`
	Level            pasturePb.EstrusLevel_Kind      `json:"level"`
	IsPeak           pasturePb.IsShow_Kind           `json:"isPeak"`
	DayHigh          int32                           `json:"dayHigh"`
	MaxHigh          int32                           `json:"maxHigh"`
	CheckResult      pasturePb.CheckResult_Kind      `json:"checkResult"`
	Remarks          string                          `json:"remarks"`
	IsShow           pasturePb.IsShow_Kind           `json:"isShow"`
	CreatedAt        int64                           `json:"createdAt"`
	UpdatedAt        int64                           `json:"updatedAt"`
}

func (n *NeckRingEstrus) TableName() string {
	return "neck_ring_estrus"
}

func NewNeckRingEstrus(
	pastureId int64,
	cow *Cow,
	exposeEstrusType pasturePb.ExposeEstrusType_Kind,
	level pasturePb.EstrusLevel_Kind,
	checkResult pasturePb.CheckResult_Kind,
	isShow pasturePb.IsShow_Kind,
) *NeckRingEstrus {
	return &NeckRingEstrus{
		PastureId:        pastureId,
		CowId:            cow.Id,
		NeckRingNumber:   cow.NeckRingNumber,
		EarNumber:        cow.EarNumber,
		Lact:             cow.Lact,
		ExposeEstrusType: exposeEstrusType,
		Level:            level,
		IsShow:           isShow,
		CheckResult:      checkResult,
	}
}

type NeckRingEstrusSlice []*NeckRingEstrus

func (e NeckRingEstrusSlice) ToPB(
	dB *kptstore.DB,
	getCowInfo func(dB *kptstore.DB, cowId int64) *Cow,
	getCowLastEvent func(DB *kptstore.DB, cowId int64, eventCategoryId pasturePb.EventCategory_Kind) *EventCowLog,
) []*pasturePb.EstrusItems {
	res := make([]*pasturePb.EstrusItems, len(e))
	for i, v := range e {
		cowInfo := getCowInfo(dB, v.CowId)
		lastEventLog := getCowLastEvent(dB, v.CowId, pasturePb.EventCategory_Breed)
		planDay, optimumMatingTime := "", ""
		lastBreedEventDetails := ""
		if lastEventLog != nil {
			lastBreedEventDetails = fmt.Sprintf("%s %s", lastEventLog.EventTypeName, lastEventLog.EventDescription)
		}
		res[i] = &pasturePb.EstrusItems{
			Id:                     int32(v.Id),
			CowId:                  int32(v.CowId),
			EarNumber:              v.EarNumber,
			DayAge:                 cowInfo.DayAge,
			Lact:                   v.Lact,
			PenName:                cowInfo.PenName,
			Status:                 v.IsShow,
			CalvingAge:             cowInfo.CalvingAge,
			PlanDay:                planDay,
			MatingTimes:            cowInfo.MatingTimes,
			OptimumMatingStartTime: optimumMatingTime,
			OptimumMatingEndTime:   optimumMatingTime,
			LastBreedEventDetails:  lastBreedEventDetails,
		}
	}
	return res
}