package handlers

import (
	"encoding/json"
	"net/http"
	"net/http/httptest"
	"testing"

	"github.com/gin-gonic/gin"
)

func TestGetStats(t *testing.T) {
	gin.SetMode(gin.TestMode)
	w := httptest.NewRecorder()
	c, _ := gin.CreateTestContext(w)
	c.Request = httptest.NewRequest("GET", "/api/stats", nil)

	// Create a mock handler with nil DB and PubSub (since we're not testing the actual DB queries)
	h := &Handler{db: nil, ps: nil}

	h.GetStats(c)

	if w.Code != http.StatusOK {
		t.Errorf("expected 200, got %d", w.Code)
	}

	var body map[string]any
	if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
		t.Fatalf("invalid json: %v", err)
	}

	// Check that all expected top-level fields are present
	expectedFields := []string{"agent_count", "plan_counts", "task_counts", "queue_counts"}
	for _, field := range expectedFields {
		if _, ok := body[field]; !ok {
			t.Errorf("missing expected field: %s", field)
		}
	}
}
