package storage

import (
	"time"

	"gorm.io/datatypes"
)

// MonitorGroup 网站分类（避免使用 MySQL 保留字 group）
type MonitorGroup struct {
	ID        string    `gorm:"primaryKey;size:64"`
	Name      string    `gorm:"size:255;not null"`
	SortOrder int       `gorm:"default:0;index"`
	CreatedAt time.Time `gorm:"autoCreateTime"`
	UpdatedAt time.Time `gorm:"autoUpdateTime"`
}

func (MonitorGroup) TableName() string { return "monitor_groups" }

// MonitorWebsite 监控站点主表
type MonitorWebsite struct {
	ID          string         `gorm:"primaryKey;size:128"`
	Name        string         `gorm:"size:255;not null;index"`
	LegacyGroup string         `gorm:"size:64;column:legacy_group"` // 旧版单分组，仅兼容
	Favicon     string         `gorm:"type:text"`
	Title       string         `gorm:"size:512"`
	IPAddresses datatypes.JSON `gorm:"type:json"` // []string
	CreatedAt   time.Time      `gorm:"autoCreateTime"`
	UpdatedAt   time.Time      `gorm:"autoUpdateTime"`

	URLs       []MonitorWebsiteURL   `gorm:"foreignKey:WebsiteID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
	GroupLinks []MonitorWebsiteGroup `gorm:"foreignKey:WebsiteID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
}

func (MonitorWebsite) TableName() string { return "monitor_websites" }

// MonitorWebsiteURL 站点监控 URL（一对多）
type MonitorWebsiteURL struct {
	ID        uint64 `gorm:"primaryKey;autoIncrement"`
	WebsiteID string `gorm:"size:128;not null;index:idx_url_site,priority:1"`
	URLID     string `gorm:"size:64;not null;index:idx_url_site,priority:2"`
	URL       string `gorm:"type:text;not null"`
	Remark    string `gorm:"size:512"`
	SortOrder int    `gorm:"default:0"`
}

func (MonitorWebsiteURL) TableName() string { return "monitor_website_urls" }

// MonitorWebsiteGroup 站点与分类多对多
type MonitorWebsiteGroup struct {
	WebsiteID string `gorm:"primaryKey;size:128"`
	GroupID   string `gorm:"primaryKey;size:64"`
}

func (MonitorWebsiteGroup) TableName() string { return "monitor_website_groups" }

// MonitorProbeLatest 每个 URL 最新一次探测结果（不逐条存历史）
type MonitorProbeLatest struct {
	WebsiteID  string    `gorm:"primaryKey;size:128"`
	URLID      string    `gorm:"primaryKey;size:64"`
	URL        string    `gorm:"type:text"`
	StatusCode int       `gorm:"column:status_code"`
	LatencyMs  int64     `gorm:"column:latency_ms"`
	IsUp       bool      `gorm:"column:is_up"`
	ErrorText  string    `gorm:"type:text;column:error_text"`
	CheckedAt  time.Time `gorm:"index"`
}

func (MonitorProbeLatest) TableName() string { return "monitor_probe_latest" }

// MonitorProbeHour 按小时汇总（用于 24h / 7d 统计与图表），行数 ≈ URL 数 × 小时数
type MonitorProbeHour struct {
	WebsiteID  string    `gorm:"primaryKey;size:128"`
	URLID      string    `gorm:"primaryKey;size:64"`
	HourAt     time.Time `gorm:"primaryKey;type:datetime"` // 整点
	ProbeCount int       `gorm:"default:0"`
	UpCount    int       `gorm:"default:0"`
	LatencySum int64     `gorm:"default:0"` // 总延迟 ms，均值为 sum/count
}

func (MonitorProbeHour) TableName() string { return "monitor_probe_hour" }

// MonitorProbeDay 按自然日汇总（用于 90 天柱状图），行数 ≈ URL 数 × 天数
type MonitorProbeDay struct {
	WebsiteID  string    `gorm:"primaryKey;size:128"`
	URLID      string    `gorm:"primaryKey;size:64"`
	StatDate   time.Time `gorm:"primaryKey;type:date"`
	ProbeCount int       `gorm:"default:0"`
	UpCount    int       `gorm:"default:0"`
	LatencySum int64     `gorm:"default:0"`
}

func (MonitorProbeDay) TableName() string { return "monitor_probe_day" }
