package handlers_test

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

	m "github.com/fiskerinc/cloud-services/pkg/common"
	"github.com/fiskerinc/cloud-services/pkg/db/queries/mocks"
	"github.com/fiskerinc/cloud-services/pkg/httpclient/tester"
	th "github.com/fiskerinc/cloud-services/pkg/testhelper"
	tr "github.com/fiskerinc/cloud-services/pkg/testrunner"
)

func TestHandleTagsAppend(t *testing.T) {
	vin := "1G1FP87S3GN100062"

	services.GetDB().SetTags(&mocks.MockTags{})

	tests := []tr.TestCase{
		{
			Name: "Basic good data test",
			HttpTestCase: &tester.HttpTestCase{
				Request: th.MakeTestRequest(http.MethodPut, "http://example.com/tags",
					handlers.TagsUpdateRequest{
						VINs: []string{vin},
						Tags: []string{"tag1"},
					}),
				ExpectedStatus: http.StatusOK,
			},
		},
		{
			Name: "Error",
			HttpTestCase: &tester.HttpTestCase{
				Request: th.MakeTestRequest(http.MethodPut, "http://example.com/tags", m.Car{
					VIN:   "1G1FP87S3GN100062",
					Model: "Ocean",
					Year:  2021,
					Trim:  "Basic",
				}),
				ExpectedStatus:   http.StatusBadRequest,
				ExpectedResponse: `{"message":"Tags required. VINs required","error":"Bad Request"}`,
			},
		},
		{
			Name: "Error - blank tag",
			HttpTestCase: &tester.HttpTestCase{
				Request: th.MakeTestRequest(http.MethodPut, "http://example.com/tags",
					handlers.TagsUpdateRequest{
						VINs: []string{vin},
						Tags: []string{"tag1", ""},
					}),
				ExpectedStatus:   http.StatusBadRequest,
				ExpectedResponse: `{"message":"Tags[1] required","error":"Bad Request"}`,
			},
		},
	}

	for _, test := range tests {
		if test.HttpTestCase != nil {
			w := test.HttpTestCase.Test(handlers.HandleTagsAppend)
			test.HttpTestCase.ValidateHttp(t, test.Name, w)
		}
	}
}
