package runtime

import "time"

// EventType represents a runtime event category broadcast to subscribers.
type EventType string

const (
	// EventTypeServersChanged is emitted whenever the set of servers or their state changes.
	EventTypeServersChanged EventType = "servers.changed"
	// EventTypeConfigReloaded is emitted after configuration reload completes.
	EventTypeConfigReloaded EventType = "config.reloaded"
	// EventTypeConfigSaved is emitted after configuration is successfully saved to disk.
	EventTypeConfigSaved EventType = "config.saved"
	// EventTypeSecretsChanged is emitted when secrets are added, updated, or deleted.
	EventTypeSecretsChanged EventType = "secrets.changed"
	// EventTypeActiveProfileChanged is emitted when the server-level default
	// active profile changes (Profiles v2). UI surfaces (Web UI, tray) refetch
	// GET /api/v1/profiles/active to reflect a switch made by another client.
	EventTypeActiveProfileChanged EventType = "active_profile.changed"
	// EventTypeOAuthTokenRefreshed is emitted when proactive token refresh succeeds.
	EventTypeOAuthTokenRefreshed EventType = "oauth.token_refreshed"
	// EventTypeOAuthRefreshFailed is emitted when proactive token refresh fails after retries.
	EventTypeOAuthRefreshFailed EventType = "oauth.refresh_failed"

	// Activity logging events (RFC-003)
	// EventTypeActivityToolCallStarted is emitted when a tool execution begins.
	EventTypeActivityToolCallStarted EventType = "activity.tool_call.started"
	// EventTypeActivityToolCallCompleted is emitted when a tool execution finishes.
	EventTypeActivityToolCallCompleted EventType = "activity.tool_call.completed"
	// EventTypeActivityPolicyDecision is emitted when a policy blocks a tool call.
	EventTypeActivityPolicyDecision EventType = "activity.policy_decision"
	// EventTypeActivityQuarantineChange is emitted when a server's quarantine state changes.
	EventTypeActivityQuarantineChange EventType = "activity.quarantine_change"

	// Spec 024: Expanded Activity Log events
	// EventTypeActivitySystemStart is emitted when MCPProxy server starts.
	EventTypeActivitySystemStart EventType = "activity.system.start"
	// EventTypeActivitySystemStop is emitted when MCPProxy server stops.
	EventTypeActivitySystemStop EventType = "activity.system.stop"
	// EventTypeActivityInternalToolCall is emitted when an internal tool (retrieve_tools, call_tool_*, etc.) completes.
	EventTypeActivityInternalToolCall EventType = "activity.internal_tool_call.completed"
	// EventTypeActivityConfigChange is emitted when configuration changes (server add/remove/update).
	EventTypeActivityConfigChange EventType = "activity.config_change"

	// Spec 026: Sensitive data detection event
	// EventTypeSensitiveDataDetected is emitted when sensitive data is detected in a tool call.
	EventTypeSensitiveDataDetected EventType = "sensitive_data.detected"

	// Spec 032: Tool-level quarantine events
	// EventTypeActivityToolQuarantineChange is emitted when a tool's quarantine status changes.
	EventTypeActivityToolQuarantineChange EventType = "activity.tool_quarantine_change"

	// Spec 039: Security scanner events
	// EventTypeSecurityScanStarted is emitted when a security scan begins.
	EventTypeSecurityScanStarted EventType = "security.scan_started"
	// EventTypeSecurityScanProgress is emitted for scanner progress updates.
	EventTypeSecurityScanProgress EventType = "security.scan_progress"
	// EventTypeSecurityScanCompleted is emitted when a security scan completes.
	EventTypeSecurityScanCompleted EventType = "security.scan_completed"
	// EventTypeSecurityScanFailed is emitted when a scanner fails.
	EventTypeSecurityScanFailed EventType = "security.scan_failed"
	// EventTypeSecurityScanSettled is the single, debounced terminal event that
	// Spec 077 US4 (MCP-2207) emits per server per scan. It collapses the
	// per-scanner scan_started/progress/completed/failed storm — including
	// repeats from reconnect storms — into one settled result.
	EventTypeSecurityScanSettled EventType = "security.scan_settled"
	// EventTypeSecurityIntegrityAlert is emitted for integrity violations.
	EventTypeSecurityIntegrityAlert EventType = "security.integrity_alert"
	// EventTypeSecurityScannerChanged is emitted when a scanner plugin's state
	// changes (e.g., background image pull started, completed, or failed).
	EventTypeSecurityScannerChanged EventType = "security.scanner_changed"
)

// Event is a typed notification published by the runtime event bus.
type Event struct {
	Type      EventType      `json:"type"`
	Timestamp time.Time      `json:"timestamp"`
	Payload   map[string]any `json:"payload,omitempty"`
}

func newEvent(eventType EventType, payload map[string]any) Event {
	return Event{
		Type:      eventType,
		Timestamp: time.Now().UTC(),
		Payload:   payload,
	}
}
