package handlers_test

import (
	"fmt"
	"net/http"
	"otaupdate/handlers"
	"otaupdate/services"
	"testing"

	"github.com/fiskerinc/cloud-services/pkg/common"
	orm "github.com/fiskerinc/cloud-services/pkg/db/queries"
	mo "github.com/fiskerinc/cloud-services/pkg/db/queries/mocks"
	th "github.com/fiskerinc/cloud-services/pkg/testhelper"
)

func TestAPITokensGetList(t *testing.T) {
	mock := mo.MockAPITokens{}
	services.GetDB().SetAPITokens(&mock)
	listData := []common.APIToken{
		{
			Token:       "TESTTOKEN",
			Roles:       "TESTROLES1,TESTROLES2",
			Description: "FOR UNIT TESTS",
		},
	}
	expectedResp := `{"data":[{"token":"TESTTOKEN","roles":"TESTROLES1,TESTROLES2","description":"FOR UNIT TESTS","expires_at":null}],"total":1}`
	expectedRespNoTotal := `{"data":[{"token":"TESTTOKEN","roles":"TESTROLES1,TESTROLES2","description":"FOR UNIT TESTS","expires_at":null}]}`
	defaultOrder := "created_at DESC"

	tests := []mo.DBHttpTest{
		{
			Name:             "No parameters",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/apitokens", nil),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: expectedResp,
			DBTestCase: mo.DBTestCase{
				ExpectedFilter: &common.APIToken{},
				ExpectedPage: &orm.PageQueryOptions{
					Order:  defaultOrder,
					Limit:  orm.PageQueryOptionsLimitMaximum,
					Offset: 0,
				},
				MockListResponse: listData,
			},
		},
		{
			Name:             "Token parameter",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/apitokens?token=TESTTOKEN", nil),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: expectedRespNoTotal,
			DBTestCase: mo.DBTestCase{
				ExpectedFilter: &common.APIToken{
					Token: "TESTTOKEN",
				},
				ExpectedPage: &orm.PageQueryOptions{
					Order:  defaultOrder,
					Limit:  orm.PageQueryOptionsLimitMaximum,
					Offset: 0,
				},
				MockListResponse: listData,
			},
		},
		{
			Name:             "ECU parameter",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/apitokens?roles=TEST", nil),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: expectedResp,
			DBTestCase: mo.DBTestCase{
				ExpectedFilter: &common.APIToken{
					Roles: "TEST",
				},
				ExpectedPage: &orm.PageQueryOptions{
					Order:  defaultOrder,
					Limit:  orm.PageQueryOptionsLimitMaximum,
					Offset: 0,
				},
				MockListResponse: listData,
			},
		},
		{
			Name:             "Paging parameters",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/apitokens?offset=10&limit=5", nil),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: expectedRespNoTotal,
			DBTestCase: mo.DBTestCase{
				ExpectedFilter: &common.APIToken{},
				ExpectedPage: &orm.PageQueryOptions{
					Order:  defaultOrder,
					Limit:  5,
					Offset: 10,
				},
				MockListResponse: listData,
			},
		},
		{
			Name:             "Wrong limit, -100",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/apitokens?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/apitokens?limit=1000", nil),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"Limit greater than 100","error":"Bad Request"}`,
		},
		{
			Name:             "Error",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/apitokens", nil),
			ExpectedStatus:   http.StatusServiceUnavailable,
			ExpectedResponse: `{"message":"something went wrong","error":"Service Unavailable"}`,
			DBTestCase: mo.DBTestCase{
				ExpectedFilter: &common.APIToken{},
				ExpectedPage: &orm.PageQueryOptions{
					Order:  defaultOrder,
					Limit:  100,
					Offset: 0,
				},
				MockError: fmt.Errorf("something went wrong"),
			},
		},
	}

	mo.RunDBTests(t, tests, handlers.HandleAPITokensGetList, &mock)
}
