package dashboard

import (
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"
)

func TestMetricsEnabledToggle(t *testing.T) {
	for _, v := range []string{"1", "true", "TRUE", "yes", "on"} {
		t.Setenv("HIVE_METRICS_ENABLED", v)
		if !metricsEnabled() {
			t.Errorf("HIVE_METRICS_ENABLED=%q should enable metrics", v)
		}
	}
	for _, v := range []string{"", "0", "false", "no", "off", "garbage"} {
		t.Setenv("HIVE_METRICS_ENABLED", v)
		if metricsEnabled() {
			t.Errorf("HIVE_METRICS_ENABLED=%q should NOT enable metrics", v)
		}
	}
}

func TestHandleMetricsExposition(t *testing.T) {
	s := covApiServer(t)
	s.deps.Config.HiveID = "test-hive"
	// The route is only registered when metrics are enabled, so exercise the
	// handler directly — the exposition format is what's under test.
	rec := httptest.NewRecorder()
	s.handleMetrics(rec, httptest.NewRequest(http.MethodGet, "/metrics", nil))
	body := rec.Body.String()
	for _, want := range []string{
		"# TYPE hive_estimated_cost_usd_total counter",
		"hive_estimated_cost_usd_total{hive_id=\"test-hive\"}",
		"# HELP hive_model_input_tokens_total",
	} {
		if !strings.Contains(body, want) {
			t.Errorf("exposition missing %q\n---\n%s", want, body)
		}
	}
	if ct := rec.Header().Get("Content-Type"); !strings.HasPrefix(ct, "text/plain") {
		t.Errorf("Content-Type = %q, want text/plain exposition", ct)
	}
}
