package handlers_test

import (
	"net/http"
	"testing"

	"otaupdate/handlers"
	"otaupdate/services"

	"github.com/fiskerinc/cloud-services/pkg/common"
	"github.com/fiskerinc/cloud-services/pkg/mongo"
	th "github.com/fiskerinc/cloud-services/pkg/testhelper"
	"github.com/fiskerinc/cloud-services/pkg/utils/elptr"
)

func TestVehicleFilterGetList(t *testing.T) {
	client, err := services.GetMongoClient()
	if err != nil {
		t.Error(err)
		return
	}
	mockMongo := mongo.NewVehiclesCollection(
		&mongo.MockCollection{
			AggregateObject: []mongo.Vehicle{
				{
					VIN: "TESTVIN123",
					CANBus: common.CANBus{
						Filters: []common.CANFilter{
							{
								CANID:    "123",
								Interval: elptr.ElPtr(100),
							},
						},
					},
				},
			},
		},
	)
	client.SetVehicles(mockMongo)

	tests := []th.BasicHttpTest{
		{
			Name:             "Invalid vin parameter",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/vehicle/TESTVIN123/filters", nil),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"primary key required","error":"Bad Request"}`,
		},
		{
			Name:             "Valid data",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/vehicle/2D4FV48T95H646760/filters", nil),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: `{"data":[{"can_id":"123","interval":100}]}`,
		},
	}

	th.RunParamHttpTests(t, tests, handlers.HandleVehicleFilterGetList, "/vehicle/:vin/filters")
}
