package models

import (
	"time"
)

// Alert 预警记录模型
type Alert struct {
	ID           uint      `gorm:"primaryKey" json:"id"`
	StudentID    uint      `json:"student_id"`
	Student      Student   `gorm:"foreignKey:StudentID" json:"student"`
	DetectionID  *uint     `json:"detection_id"`
	Detection    *Detection `gorm:"foreignKey:DetectionID" json:"detection"`
	AlertLevel   int       `json:"alert_level"` // 1:关注, 2:预警, 3:告警
	AlertType    string    `gorm:"type:varchar(32)" json:"alert_type"` // vision_drop, fatigue_high, abnormal
	AlertContent string    `gorm:"type:text" json:"alert_content"`
	Status       int       `gorm:"default:0" json:"status"` // 0:未处理, 1:已通知, 2:已处理
	NotifiedAt   *time.Time `json:"notified_at"`
	HandledAt    *time.Time `json:"handled_at"`
	HandlerID    *uint     `json:"handler_id"` // 处理人ID
	HandleRemark string    `gorm:"type:text" json:"handle_remark"`
	CreatedAt    time.Time `json:"created_at"`
	UpdatedAt    time.Time `json:"updated_at"`
}

// AlertConfig 预警配置模型
type AlertConfig struct {
	ID                  uint      `gorm:"primaryKey" json:"id"`
	SchoolID            *uint     `json:"school_id"`
	School              *School   `gorm:"foreignKey:SchoolID" json:"school"`
	AlertLevel          int       `json:"alert_level"` // 1:关注, 2:预警, 3:告警
	VisionThreshold     float64   `json:"vision_threshold"`     // 视力阈值
	DropThreshold       float64   `json:"drop_threshold"`       // 下降幅度阈值
	NotifyParent        bool      `gorm:"default:true" json:"notify_parent"` // 通知家长
	NotifyTeacher       bool      `gorm:"default:true" json:"notify_teacher"` // 通知老师
	NotifySchoolDoctor  bool      `gorm:"default:false" json:"notify_school_doctor"` // 通知校医
	CreatedAt           time.Time `json:"created_at"`
	UpdatedAt           time.Time `json:"updated_at"`
}

// AlertSummary 预警摘要
type AlertSummary struct {
	ID              uint      `gorm:"primaryKey" json:"id"`
	EntityID        uint      `json:"entity_id"` // 学生/班级/学校的ID
	EntityType      string    `gorm:"type:varchar(20)" json:"entity_type"` // student, class, school
	TotalAlerts     int       `json:"total_alerts"`
	HighRiskCount   int       `json:"high_risk_count"`   // 高风险数量
	MediumRiskCount int       `json:"medium_risk_count"` // 中风险数量
	LowRiskCount    int       `json:"low_risk_count"`    // 低风险数量
	LastAlertDate   *time.Time `json:"last_alert_date"`
	LastAlertType   string    `gorm:"type:varchar(32)" json:"last_alert_type"`
	CreatedAt       time.Time `json:"created_at"`
	UpdatedAt       time.Time `json:"updated_at"`
}

// AlertNotification 预警通知模型
type AlertNotification struct {
	ID          uint      `gorm:"primaryKey" json:"id"`
	AlertID     uint      `json:"alert_id"`
	Alert       Alert     `gorm:"foreignKey:AlertID" json:"alert"`
	RecipientID uint      `json:"recipient_id"` // 接收者ID
	RecipientType string  `gorm:"type:varchar(20)" json:"recipient_type"` // parent, teacher, school_doctor
	Channel     string    `gorm:"type:varchar(20)" json:"channel"` // sms, email, app_push
	Title       string    `gorm:"type:varchar(255)" json:"title"`
	Content     string    `gorm:"type:text" json:"content"`
	SentAt      time.Time `json:"sent_at"`
	ReadAt      *time.Time `json:"read_at"`
	Status      int       `gorm:"default:0" json:"status"` // 0:待发送, 1:已发送, 2:已读
	CreatedAt   time.Time `json:"created_at"`
}

// AlertDistribution 预警分布统计
type AlertDistribution struct {
	ID        uint      `gorm:"primaryKey" json:"id"`
	Date      time.Time `json:"date"`
	EntityType string   `gorm:"type:varchar(20)" json:"entity_type"` // student, class, school
	EntityID  uint      `json:"entity_id"`
	GreenCount  int     `json:"green_count"`  // 绿色预警数量
	YellowCount int     `json:"yellow_count"` // 黄色预警数量
	OrangeCount int     `json:"orange_count"` // 橙色预警数量
	RedCount    int     `json:"red_count"`    // 红色预警数量
	CreatedAt   time.Time `json:"created_at"`
}

// VisionDistribution 视力分布统计
type VisionDistribution struct {
	ID         uint      `gorm:"primaryKey" json:"id"`
	Date       time.Time `json:"date"`
	EntityType string    `gorm:"type:varchar(20)" json:"entity_type"` // student, class, school
	EntityID   uint      `json:"entity_id"`
	NormalCount int      `json:"normal_count"`       // 正常人数
	MildMyopiaCount int  `json:"mild_myopia_count"`  // 轻度近视人数
	ModerateMyopiaCount int `json:"moderate_myopia_count"` // 中度近视人数
	SevereMyopiaCount int   `json:"severe_myopia_count"`   // 高度近视人数
	CreatedAt  time.Time `json:"created_at"`
}

// JSONMap 自定义JSON类型（已在其他模型中定义，这里引用）
// 为避免重复定义，我们可以将其移到公共包中

// AlertType 预警类型枚举
const (
	AlertTypeVisionDrop      = "vision_drop"      // 视力下降
	AlertTypeFatigueHigh     = "fatigue_high"     // 疲劳度过高
	AlertTypeAbnormalBehavior = "abnormal_behavior" // 异常行为
	AlertTypeDeviceError     = "device_error"     // 设备异常
)

// AlertLevel 预警级别枚举
const (
	AlertLevelGreen  = 0 // 正常
	AlertLevelYellow = 1 // 关注
	AlertLevelOrange = 2 // 预警
	AlertLevelRed    = 3 // 告警
)

// AlertStatus 预警状态枚举
const (
	AlertStatusUnhandled = 0 // 未处理
	AlertStatusNotified  = 1 // 已通知
	AlertStatusHandled   = 2 // 已处理
)