// Package metrics defines all Prometheus metrics for Switchboard Core.
// All metrics use the "switchboard_" prefix. Registered via promauto
// so they are available on the default registry's /metrics endpoint.
package metrics

import (
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promauto"
)

// ── HTTP Request Metrics ────────────────────

var (
	HTTPRequestsTotal = promauto.NewCounterVec(prometheus.CounterOpts{
		Name: "switchboard_http_requests_total",
		Help: "Total HTTP requests by method, path pattern, and status code.",
	}, []string{"method", "path_pattern", "status"})

	HTTPRequestDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
		Name:    "switchboard_http_request_duration_seconds",
		Help:    "HTTP request latency in seconds.",
		Buckets: []float64{0.01, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 30},
	}, []string{"method", "path_pattern"})
)

// ── WebSocket Metrics ───────────────────────

var WebSocketConnections = promauto.NewGauge(prometheus.GaugeOpts{
	Name: "switchboard_websocket_connections",
	Help: "Current number of active WebSocket connections.",
})

// ── Completion / Token Metrics ──────────────

var (
	CompletionTokensTotal = promauto.NewCounterVec(prometheus.CounterOpts{
		Name: "switchboard_completion_tokens_total",
		Help: "Total tokens processed by direction (prompt|completion) and model.",
	}, []string{"direction", "model_id"})

	CompletionsTotal = promauto.NewCounterVec(prometheus.CounterOpts{
		Name: "switchboard_completions_total",
		Help: "Total completion requests by provider, model, and status.",
	}, []string{"provider_config_id", "model_id", "status"})

	CompletionDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
		Name:    "switchboard_completion_duration_seconds",
		Help:    "Completion request latency (wall time including tool loops).",
		Buckets: []float64{0.1, 0.5, 1, 2, 5, 10, 30, 60, 120},
	}, []string{"provider_config_id", "model_id"})
)

// ── Provider Health ─────────────────────────

var ProviderStatus = promauto.NewGaugeVec(prometheus.GaugeOpts{
	Name: "switchboard_provider_status",
	Help: "Provider health status: 0=unknown, 1=healthy, 2=degraded, 3=down.",
}, []string{"provider_config_id"})

// ── Database Pool Metrics ───────────────────

var (
	DBOpenConnections = promauto.NewGauge(prometheus.GaugeOpts{
		Name: "switchboard_db_open_connections",
		Help: "Number of open database connections.",
	})
	DBInUseConnections = promauto.NewGauge(prometheus.GaugeOpts{
		Name: "switchboard_db_in_use_connections",
		Help: "Number of database connections currently in use.",
	})
	DBIdleConnections = promauto.NewGauge(prometheus.GaugeOpts{
		Name: "switchboard_db_idle_connections",
		Help: "Number of idle database connections.",
	})
	DBWaitCount = promauto.NewGauge(prometheus.GaugeOpts{
		Name: "switchboard_db_wait_count_total",
		Help: "Total number of connections waited for.",
	})
	DBWaitDuration = promauto.NewGauge(prometheus.GaugeOpts{
		Name: "switchboard_db_wait_duration_seconds_total",
		Help: "Total time blocked waiting for a new connection (seconds).",
	})
)

// ── Task Scheduler Metrics ──────────────────

var TaskExecutionsTotal = promauto.NewCounterVec(prometheus.CounterOpts{
	Name: "switchboard_task_executions_total",
	Help: "Total task executions by status (success|error).",
}, []string{"status"})

// ── Sandbox Metrics ─────────────────────────

var SandboxExecutionsTotal = promauto.NewCounterVec(prometheus.CounterOpts{
	Name: "switchboard_sandbox_executions_total",
	Help: "Total Starlark sandbox executions by entry point and status.",
}, []string{"entry_point", "status"})
