package health

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

	"github.com/stretchr/testify/assert"
	"go.mongodb.org/mongo-driver/mongo"

	"github.com/pfeifferj/container-mom-go/pkg/operator/mongodb/mock"
)

type mockOperator struct {
	mongodb *mock.MockMongoDB
}

func (m *mockOperator) GetMongoDB() *mongo.Client {
	return m.mongodb.GetClient()
}

func TestHealthChecks(t *testing.T) {
	// Create mock MongoDB
	mockMongo := mock.NewMockMongoDB()

	mockOp := &mockOperator{
		mongodb: mockMongo,
	}

	handler := &Handler{Operator: mockOp}

	t.Run("healthz", func(t *testing.T) {
		req := httptest.NewRequest(http.MethodGet, "/healthz", nil)
		w := httptest.NewRecorder()

		handler.Healthz(w, req)

		assert.Equal(t, http.StatusOK, w.Code)
	})

	t.Run("readyz", func(t *testing.T) {
		req := httptest.NewRequest(http.MethodGet, "/readyz", nil)
		w := httptest.NewRecorder()

		handler.Readyz(w, req)

		// Should fail since mock returns nil client
		assert.Equal(t, http.StatusServiceUnavailable, w.Code)
	})
}
