package health

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

	"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 nil // Mock doesn't need real client
}

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) {
		// Skip if MongoDB is not available
		ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
		defer cancel()
		if err := mockMongo.Ping(ctx, nil); err != nil {
			t.Skip("MongoDB not available:", err)
		}

		req := httptest.NewRequest(http.MethodGet, "/readyz", nil)
		w := httptest.NewRecorder()

		handler.Readyz(w, req)

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