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 TestFleetFilterGetList(t *testing.T) {
	client, err := services.GetMongoClient()
	if err != nil {
		t.Error(err)
		return
	}
	mockMongo := mongo.NewFleetsCollection(
		&mongo.MockCollection{
			AggregateObject: []mongo.Fleet{
				{
					Name: "US-TEST",
					CANBus: common.CANBus{
						Filters: []common.CANFilter{
							{
								CANID:    "123",
								Interval: elptr.ElPtr(100),
							},
						},
					},
				},
			},
		},
	)
	client.SetFleets(mockMongo)

	tests := []th.BasicHttpTest{
		{
			Name:             "Invalid name parameter",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/fleet/$TEST/filters", nil),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"primary key required","error":"Bad Request"}`,
		},
		{
			Name:             "Valid data",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/fleet/US-TEST/filters", nil),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: `{"data":[{"can_id":"123","interval":100}]}`,
		},
		{
			Name:             "Valid data limit 50",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/fleet/US-TEST/filters?limit=50", nil),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: `{"data":[{"can_id":"123","interval":100}]}`,
		},
		{
			Name:             "Wrong limit, -100",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/fleet/US-TEST/filters?limit=-100", nil),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"Limit less than 0","error":"Bad Request"}`,
		},
		{
			Name:             "Wrong limit, 1000",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/fleet/US-TEST/filters?limit=1000", nil),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"Limit greater than 100","error":"Bad Request"}`,
		},
	}

	th.RunParamHttpTests(t, tests, handlers.HandleFleetFilterGetList, "/fleet/:name/filters")
}
