package models

import (
	"time"

	"gorm.io/datatypes"
)

/*
	所有与药品、疫苗和用药记录相关的模型
*/

// MedicationType 定义了兽药的类型
type MedicationType string

const (
	Powder    MedicationType = "粉剂"
	Injection MedicationType = "针剂"
	Vaccine   MedicationType = "疫苗"
)

// MedicationCategory 定义了兽药的种类
type MedicationCategory string

const (
	Tetracycline      MedicationCategory = "四环素类"
	Sulfonamide       MedicationCategory = "磺胺类"
	Penicillin        MedicationCategory = "青霉素类"
	Macrolide         MedicationCategory = "大环内酯类"
	Quinolone         MedicationCategory = "喹诺酮类"
	Anthelmintic      MedicationCategory = "驱虫药"
	Disinfectant      MedicationCategory = "消毒药"
	BiologicalProduct MedicationCategory = "生物制品"
)

// MixType 定义了粉剂药物该如何混合
type MixType string

const (
	MixFeed  = "饲料加药"
	MixWater = "水中加药"
)

// PowderInstructions 定义了粉剂使用说明.
// 在程序中, 可以将 Medication.Instructions 字段反序列化为此结构进行操作.
type PowderInstructions struct {
	// 出栏前停药期
	WithdrawalPeriod time.Duration `json:"withdrawal_period"`
	// 拌料使用计量, 每千克体重用多少克药, 单位: g/kg
	BodyWeightDosageUsed float32 `json:"body_weight_dosage_used"`
	// 拌料使用剂量, 每升水加多少克药或每千克饲料干重加多少克药, 单位: g/kg(L)
	MixDosageUsed float32 `json:"mix_dosage_used"`
	// 拌料使用方式, 兑水/拌料
	MixType MixType `json:"mix_type"`
}

// Medication 定义了兽药/疫苗的基本信息模型
type Medication struct {
	Model
	Name                          string             `gorm:"size:100;not null;comment:药品名称" json:"name"`
	Type                          MedicationType     `gorm:"size:20;not null;comment:兽药类型 (粉剂, 针剂, 疫苗)" json:"type"`
	Category                      MedicationCategory `gorm:"size:30;not null;comment:兽药种类 (四环素类, 磺胺类等)" json:"category"`
	DosagePerUnit                 float32            `gorm:"size:50;comment:一份药物的计量 (针剂计量单位为毫升, 粉剂为克)" json:"dosage_per_unit"`
	ActiveIngredientConcentration float32            `gorm:"size:50;comment:有效成分含量百分比" json:"active_ingredient_concentration"`
	Manufacturer                  string             `gorm:"size:100;comment:生产厂家" json:"manufacturer"`
	Instructions                  datatypes.JSON     `gorm:"type:jsonb;comment:使用说明" json:"instructions"`
}

func (Medication) TableName() string {
	return "medications"
}

// MedicationReasonType 定义了用药原因
type MedicationReasonType string

const (
	ReasonTypePreventive MedicationReasonType = "预防"
	ReasonTypeTreatment  MedicationReasonType = "治疗"
	ReasonTypeHealthCare MedicationReasonType = "保健"
)

// MedicationLog 记录了对整个猪批次的用药情况
type MedicationLog struct {
	Model
	PigBatchID   uint32               `gorm:"not null;index;comment:关联的猪批次ID"`
	MedicationID uint32               `gorm:"not null;index;comment:关联的药品ID"`
	Medication   Medication           `gorm:"foreignKey:MedicationID"` // 预加载药品信息
	DosageUsed   float32              `gorm:"not null;comment:使用的总剂量 (单位由药品决定,如g或ml)"`
	TargetCount  int                  `gorm:"not null;comment:用药对象数量"`
	Reason       MedicationReasonType `gorm:"size:20;not null;comment:用药原因"`
	Description  string               `gorm:"size:255;comment:具体描述,如'治疗呼吸道病'"`
	OperatorID   uint32               `gorm:"comment:操作员ID"`
	HappenedAt   time.Time            `gorm:"primaryKey;comment:用药时间"`
}

func (MedicationLog) TableName() string {
	return "medication_logs"
}
