package models

import (
	"time"
)

// Device 设备模型
type Device struct {
	ID              uint      `gorm:"primaryKey" json:"id"`
	DeviceNo        string    `gorm:"type:varchar(64);uniqueIndex" json:"device_no"`
	DeviceName      string    `gorm:"type:varchar(128)" json:"device_name"`
	DeviceType      string    `gorm:"type:varchar(32)" json:"device_type"` // terminal, camera, edge_box, gateway
	SchoolID        *uint     `json:"school_id"`
	School          *School   `gorm:"foreignKey:SchoolID" json:"school"`
	ClassID         *uint     `json:"class_id"`
	Class           *Class    `gorm:"foreignKey:ClassID" json:"class"`
	IPAddress       string    `gorm:"type:varchar(45)" json:"ip_address"`
	MacAddress      string    `gorm:"type:varchar(32)" json:"mac_address"`
	Status          int       `json:"status"` // 0:离线, 1:在线, 2:故障, 3:维护中
	LastHeartbeat   *time.Time `json:"last_heartbeat"`
	FirmwareVersion string    `gorm:"type:varchar(32)" json:"firmware_version"`
	ConfigVersion   int       `json:"config_version"`
	Attributes      JSONMap   `gorm:"type:json" json:"attributes"` // 设备属性
	CreatedAt       time.Time `json:"created_at"`
	UpdatedAt       time.Time `json:"updated_at"`

	// 关联的设备日志
	DeviceLogs []DeviceLog `gorm:"foreignKey:DeviceID" json:"device_logs"`
}

// DeviceConfig 设备配置模型
type DeviceConfig struct {
	ID         uint      `gorm:"primaryKey" json:"id"`
	DeviceID   uint      `json:"device_id"`
	Device     Device    `gorm:"foreignKey:DeviceID" json:"device"`
	Settings   JSONMap   `gorm:"type:json" json:"settings"` // 配置项
	Version    int       `json:"version"`                   // 配置版本
	UpdatedAt  time.Time `json:"updated_at"`
}

// DeviceLog 设备日志模型
type DeviceLog struct {
	ID         uint      `gorm:"primaryKey" json:"id"`
	DeviceID   uint      `json:"device_id"`
	Device     Device    `gorm:"foreignKey:DeviceID" json:"device"`
	LogType    string    `gorm:"type:varchar(32)" json:"log_type"` // status, command, error
	Content    JSONMap   `gorm:"type:json" json:"content"`         // 日志内容
	CreatedAt  time.Time `json:"created_at"`
}

// DeviceStatusInfo 设备状态信息模型
type DeviceStatusInfo struct {
	ID           uint      `gorm:"primaryKey" json:"id"`
	DeviceID     uint      `json:"device_id"`
	Device       Device    `gorm:"foreignKey:DeviceID" json:"device"`
	Status       int       `json:"status"` // 0:离线, 1:在线, 2:故障, 3:维护中
	CPUUsage     float64   `json:"cpu_usage"`
	MemoryUsage  float64   `json:"memory_usage"`
	DiskUsage    float64   `json:"disk_usage"`
	NetworkStatus string   `gorm:"type:varchar(32)" json:"network_status"`
	CameraStatus string   `gorm:"type:varchar(32)" json:"camera_status"`
	Temperature  float64   `json:"temperature"`
	FirmwareVersion string `gorm:"type:varchar(32)" json:"firmware_version"`
	HealthInfo   JSONMap   `gorm:"type:json" json:"health_info"` // 健康信息
	ReportedAt   time.Time `json:"reported_at"`
}

// DeviceCommand 设备指令模型
type DeviceCommand struct {
	ID             uint      `gorm:"primaryKey" json:"id"`
	DeviceID       uint      `json:"device_id"`
	Device         Device    `gorm:"foreignKey:DeviceID" json:"device"`
	CommandType    string    `gorm:"type:varchar(64)" json:"command_type"`
	Params         JSONMap   `gorm:"type:json" json:"params"` // 参数
	Timeout        int       `json:"timeout"`                 // 超时时间
	CreatedAt      time.Time `json:"created_at"`
	ExecutedAt     *time.Time `json:"executed_at"`
	ExecutionResult string   `gorm:"type:text" json:"execution_result"` // 执行结果
	Status         int       `json:"status"` // 0:待执行, 1:执行中, 2:成功, 3:失败
}

// DeviceMessage 设备消息模型
type DeviceMessage struct {
	ID          uint      `gorm:"primaryKey" json:"id"`
	DeviceID    uint      `json:"device_id"`
	Device      Device    `gorm:"foreignKey:DeviceID" json:"device"`
	MessageType string    `gorm:"type:varchar(32)" json:"message_type"` // detection.data, device.status, device.command
	Header      JSONMap   `gorm:"type:json" json:"header"`             // 消息头
	Payload     JSONMap   `gorm:"type:json" json:"payload"`            // 消息体
	Signature   string    `gorm:"type:varchar(255)" json:"signature"`  // 签名
	CreatedAt   time.Time `json:"created_at"`
}

// DeviceHeartbeat 设备心跳模型
type DeviceHeartbeat struct {
	ID         uint      `gorm:"primaryKey" json:"id"`
	DeviceID   uint      `json:"device_id"`
	Device     Device    `gorm:"foreignKey:DeviceID" json:"device"`
	IPAddress  string    `gorm:"type:varchar(45)" json:"ip_address"`
	LastSeen   time.Time `json:"last_seen"`
	Status     int       `json:"status"` // 0:离线, 1:在线
}

// DeviceGroup 设备分组模型
type DeviceGroup struct {
	ID        uint      `gorm:"primaryKey" json:"id"`
	Name      string    `gorm:"type:varchar(128)" json:"name"`
	Type      string    `gorm:"type:varchar(32)" json:"type"` // classroom, school, campus
	EntityID  uint      `json:"entity_id"` // 关联的学校或班级ID
	Entity    string    `gorm:"type:varchar(32)" json:"entity"` // school, class
	Description string  `gorm:"type:text" json:"description"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`

	// 关联的设备
	Devices []Device `gorm:"many2many:device_group_relations;" json:"devices"`
}

// DeviceGroupRelation 设备与分组关联模型
type DeviceGroupRelation struct {
	ID         uint      `gorm:"primaryKey" json:"id"`
	DeviceID   uint      `json:"device_id"`
	GroupID    uint      `json:"group_id"`
	CreatedAt  time.Time `json:"created_at"`
}

// DeviceMaintenance 设备维护模型
type DeviceMaintenance struct {
	ID          uint      `gorm:"primaryKey" json:"id"`
	DeviceID    uint      `json:"device_id"`
	Device      Device    `gorm:"foreignKey:DeviceID" json:"device"`
	Type        string    `gorm:"type:varchar(32)" json:"type"` // repair, upgrade, calibration
	Description string    `gorm:"type:text" json:"description"`
	StartedAt   time.Time `json:"started_at"`
	EndedAt     *time.Time `json:"ended_at"`
	Status      int       `json:"status"` // 0:待处理, 1:进行中, 2:已完成, 3:已取消
	Technician  string    `gorm:"type:varchar(64)" json:"technician"`
	Notes       string    `gorm:"type:text" json:"notes"`
	CreatedAt   time.Time `json:"created_at"`
	UpdatedAt   time.Time `json:"updated_at"`
}

// DeviceType 设备类型枚举
const (
	DeviceTypeTerminal = "terminal"  // 终端设备（触摸屏一体机）
	DeviceTypeCamera   = "camera"    // 摄像头
	DeviceTypeEdgeBox  = "edge_box"  // 边缘计算盒子
	DeviceTypeGateway  = "gateway"   // 网关设备
)

// DeviceStatus 设备状态枚举
const (
	DeviceStatusOffline     = 0 // 离线
	DeviceStatusOnline      = 1 // 在线
	DeviceStatusFault       = 2 // 故障
	DeviceStatusMaintenance = 3 // 维护中
)

// DeviceMessageType 设备消息类型枚举
const (
	DeviceMessageTypeStatus   = "device.status"   // 设备状态
	DeviceMessageTypeCommand  = "device.command"  // 控制指令
	DeviceMessageTypeDetection = "detection.data" // 检测数据
	DeviceMessageTypeAlert    = "alert"          // 预警事件
	DeviceMessageTypeConfig   = "config"         // 配置更新
	DeviceMessageTypeHeartbeat = "heartbeat"      // 心跳上报
)

// DeviceCommandStatus 指令状态枚举
const (
	DeviceCommandStatusPending  = 0 // 待执行
	DeviceCommandStatusExecuting = 1 // 执行中
	DeviceCommandStatusSuccess  = 2 // 成功
	DeviceCommandStatusFailed   = 3 // 失败
)

// DeviceMaintenanceType 维护类型枚举
const (
	DeviceMaintenanceTypeRepair      = "repair"      // 维修
	DeviceMaintenanceTypeUpgrade     = "upgrade"     // 升级
	DeviceMaintenanceTypeCalibration = "calibration" // 校准
)