package handlers_test

import (
	"context"
	"net/http"
	"net/http/httptest"
	"otaupdate/handlers"
	"otaupdate/services"
	"testing"

	"github.com/fiskerinc/cloud-services/pkg/clickhouse"
	"github.com/fiskerinc/cloud-services/pkg/common"
	"github.com/stretchr/testify/assert"
)

func TestHandleCanSignalListGetList(t *testing.T) {
	tests := map[string]struct {
		conn      clickhouse.ConnInterface
		expStatus int
		expBody   string
	}{
		"correct": {
			conn:      &clickhouse.MockConn{ExpectedResult: []common.CANSignalNameList{{"A"}, {"B"}},},
			expStatus: http.StatusOK,
			expBody:   `{"data":[{"signal_name":"A"},{"signal_name":"B"}]}`,
		},
		"failed_query": {
			conn:      &clickhouse.MockConn{ExpectedResult: someErr},
			expStatus: http.StatusServiceUnavailable,
			expBody:   `{"message":"json: cannot unmarshal object into Go value of type []common.CANSignalNameList","error":"Service Unavailable"}`,
		},
	}

	for tname, tt := range tests {
		t.Run(tname, func(t *testing.T) {
			services.SetClickhouseConn(tt.conn)
			w := httptest.NewRecorder()

			ctx := context.Background()
			r := httptest.NewRequest(http.MethodGet, "http://example.com/can_signals_list", nil).
				WithContext(ctx)

			handlers.HandleCanSignalListGet(w, r)
			assert.Equal(t, tt.expStatus, w.Code)
			assert.Equal(t, tt.expBody, w.Body.String())
		})
	}
}
