package model

import (
	"time"

	"gorm.io/gorm"
)

// AlertType defines the type of alert
type AlertType string

const (
	AlertTypeRateLimit     AlertType = "rate_limit"
	AlertTypeErrorSpike    AlertType = "error_spike"
	AlertTypeQuotaExceeded AlertType = "quota_exceeded"
	AlertTypeKeyDisabled   AlertType = "key_disabled"
	AlertTypeKeyExpired    AlertType = "key_expired"
	AlertTypeProviderDown  AlertType = "provider_down"
	AlertTypeTrafficSpike  AlertType = "traffic_spike"
)

// AlertSeverity defines the severity level of an alert
type AlertSeverity string

const (
	AlertSeverityInfo     AlertSeverity = "info"
	AlertSeverityWarning  AlertSeverity = "warning"
	AlertSeverityCritical AlertSeverity = "critical"
)

// AlertStatus defines the status of an alert
type AlertStatus string

const (
	AlertStatusActive       AlertStatus = "active"
	AlertStatusAcknowledged AlertStatus = "acknowledged"
	AlertStatusResolved     AlertStatus = "resolved"
	AlertStatusDismissed    AlertStatus = "dismissed"
)

// Alert represents a system alert or notification
type Alert struct {
	gorm.Model
	Type        AlertType     `gorm:"size:50;not null;index" json:"type"`
	Severity    AlertSeverity `gorm:"size:20;not null;index" json:"severity"`
	Status      AlertStatus   `gorm:"size:20;not null;default:'active';index" json:"status"`
	Title       string        `gorm:"size:255;not null" json:"title"`
	Message     string        `gorm:"type:text" json:"message"`
	RelatedID   uint          `gorm:"index" json:"related_id,omitempty"`
	RelatedType string        `gorm:"size:50" json:"related_type,omitempty"` // master, key, apikey, provider_group
	RelatedName string        `gorm:"size:255" json:"related_name,omitempty"`
	Fingerprint string        `gorm:"size:255;index" json:"fingerprint,omitempty"` // For deduplication: type:related_type:related_id
	Metadata    string        `gorm:"type:text" json:"metadata,omitempty"`         // JSON encoded additional data
	AckedAt     *time.Time    `json:"acked_at,omitempty"`
	AckedBy     string        `gorm:"size:100" json:"acked_by,omitempty"`
	ResolvedAt  *time.Time    `json:"resolved_at,omitempty"`
	ExpiresAt   *time.Time    `gorm:"index" json:"expires_at,omitempty"` // Auto-dismiss after this time
}
