package model

import (
	"encoding/json"

	"gitee.com/xuyiping_admin/pkg/logger/zaplog"
	"go.uber.org/zap"

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

type EventCowTreatment struct {
	Id                 int64                          `json:"id"`
	PastureId          int64                          `json:"pastureId"`
	CowId              int64                          `json:"cowId"`
	CowDiseaseId       int64                          `json:"cowDiseaseId"`
	DiseaseId          int64                          `json:"diseaseId"`
	DiseaseName        string                         `json:"diseaseName"`
	DiseaseType        int32                          `json:"diseaseType"`
	DiseaseTypeName    string                         `json:"diseaseTypeName"`
	PrescriptionId     int32                          `json:"prescriptionId"`
	PrescriptionName   string                         `json:"prescriptionName"`
	PrescriptionDetail string                         `json:"prescriptionDetail"`
	TreatmentResult    pasturePb.TreatmentResult_Kind `json:"treatmentResult"`
	OperationId        int64                          `json:"operationId"`
	OperationName      string                         `json:"operationName"`
	MessageId          int64                          `json:"messageId"`
	MessageName        string                         `json:"messageName"`
	Remarks            string                         `json:"remarks"`
	TreatmentAt        int64                          `json:"treatmentAt"`
	CreatedAt          int64                          `json:"createdAt"`
	UpdatedAt          int64                          `json:"updatedAt"`
}

func (e *EventCowTreatment) TableName() string {
	return "event_cow_treatment"
}

func NewEventCowTreatment(
	pastureId int64,
	prescription *Prescription,
	req *pasturePb.CowTreatmentRequest,
	diseaseTypeMap map[int32]string,
	operation, currentUser *SystemUser,
) *EventCowTreatment {
	b, _ := json.Marshal(req.PrescriptionDetail)
	return &EventCowTreatment{
		PastureId:          pastureId,
		CowId:              int64(req.CowId),
		CowDiseaseId:       int64(req.Id),
		DiseaseId:          int64(req.DiseaseId),
		DiseaseName:        req.DiseaseName,
		DiseaseType:        req.DiseaseType,
		DiseaseTypeName:    diseaseTypeMap[req.DiseaseType],
		PrescriptionId:     req.PrescriptionId,
		PrescriptionName:   prescription.Name,
		PrescriptionDetail: string(b),
		TreatmentResult:    req.TreatmentResult,
		OperationId:        operation.Id,
		OperationName:      operation.Name,
		MessageId:          currentUser.Id,
		MessageName:        currentUser.Name,
		Remarks:            req.Remarks,
	}
}

func NewEventCowCurableTreatment(pastureId int64, currUser, operationUser *SystemUser, cowDisease *EventCowDisease, remarks string, curableAt int64) *EventCowTreatment {
	return &EventCowTreatment{
		PastureId:       pastureId,
		CowId:           cowDisease.CowId,
		CowDiseaseId:    cowDisease.Id,
		DiseaseId:       int64(cowDisease.DiseaseId),
		DiseaseName:     cowDisease.DiseaseName,
		TreatmentResult: pasturePb.TreatmentResult_Curable,
		OperationId:     operationUser.Id,
		OperationName:   operationUser.Name,
		Remarks:         remarks,
		TreatmentAt:     curableAt,
	}
}

type EventCowTreatmentSlice []*EventCowTreatment

func (e EventCowTreatmentSlice) ToPB(eventCowDiseaseList []*EventCowDisease) []*pasturePb.EventCowTreatment {
	res := make([]*pasturePb.EventCowTreatment, len(e))
	for i, v := range e {
		prescriptionDetail := make([]*pasturePb.PrescriptionDrugsList, 0)
		if v.PrescriptionDetail != "" {
			err := json.Unmarshal([]byte(v.PrescriptionDetail), &prescriptionDetail)
			if err != nil {
				zaplog.Error("EventCowTreatmentSlice", zap.Any("err", err))
			}
		}

		eventCowTreatment := &pasturePb.EventCowTreatment{
			CowId:              int32(v.CowId),
			CowDiseaseId:       int32(v.CowDiseaseId),
			PrescriptionId:     v.PrescriptionId,
			PrescriptionName:   v.PrescriptionName,
			PrescriptionDetail: prescriptionDetail,
			TreatmentResult:    0,
			OperationId:        int32(v.OperationId),
			OperationName:      v.OperationName,
			Remarks:            v.Remarks,
			CreatedAt:          int32(v.CreatedAt),
			UpdatedAt:          int32(v.UpdatedAt),
			DiseaseId:          int32(v.DiseaseId),
			Id:                 int32(v.Id),
		}
		for _, d := range eventCowDiseaseList {
			if v.CowDiseaseId != d.Id {
				continue
			}
			eventCowTreatment.DiseaseName = d.DiseaseName
			eventCowTreatment.DiseaseAt = int32(d.DiseaseAt)
		}
		res[i] = eventCowTreatment
	}
	return res
}