package handlers_test

import (
	"net/http"
	"testing"

	"otaupdate/handlers"
	"otaupdate/services"

	"github.com/fiskerinc/cloud-services/pkg/common"
	"github.com/fiskerinc/cloud-services/pkg/db/queries/mocks"
	"github.com/fiskerinc/cloud-services/pkg/redis"
	rm "github.com/fiskerinc/cloud-services/pkg/redis/tester"
	th "github.com/fiskerinc/cloud-services/pkg/testhelper"
)

func TestHandleVehicleConnectionStatuses(t *testing.T) {
	redis.MockRedisConnection()
	mockRedis := rm.MockRedis{
		SISMEMBEResults: map[string]map[string]interface{}{
			redis.CarSessionsKey(): {
				"1G1FP87S3GN100062": int64(0),
				"3C4PDCBG0ET127145": int64(1),
			},
			redis.HMISessionsKey(): {
				"1G1FP87S3GN100062": int64(0),
				"3C4PDCBG0ET127145": int64(1),
			},
		},
	}
	services.SetRedisClientPool(rm.NewMockClientPool(&mockRedis))

	tests := []mocks.DBHttpTest{
		{
			Name: "Good data",
			Request: th.MakeTestRequest(http.MethodPost, "http://example.com/carsconnected", common.VINs{
				VINs: []string{"1G1FP87S3GN100062", "3C4PDCBG0ET127145"},
			}),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: `{"1G1FP87S3GN100062":false,"2:1G1FP87S3GN100062":false,"2:3C4PDCBG0ET127145":true,"3C4PDCBG0ET127145":true}`,
		},
		{
			Name:             "No data",
			Request:          th.MakeTestRequest(http.MethodPost, "http://example.com/carsconnected", nil),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"VINs required","error":"Bad Request"}`,
		},
		{
			Name: "Bad VIN data",
			Request: th.MakeTestRequest(http.MethodPost, "http://example.com/carsconnected", common.VINs{
				VINs: []string{"1G1FP87S3GN100062", "XXXXXX"},
			}),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"VINs[1] 'XXXXXX' invalid","error":"Bad Request"}`,
		},
	}

	mocks.RunDBTests(t, tests, handlers.HandleVehicleConnectionStatuses, nil)
}
