package handlers_test

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

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

func TestHandleVersionGet(t *testing.T) {
	mock := mo.MockCarVersionsLog{
		GetCarVersionsResult: map[string]string{
			"DBC":  "dbc-version",
			"TREX": "trex-version",
		},
	}
	services.GetDB().SetCarVersionsLog(&mock)

	tests := []th.BasicHttpTest{
		{
			Name:             "Invalid VIN",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/vehicle/1111/version", nil),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"VIN '1111' invalid","error":"Bad Request"}`,
		},
		{
			Name:             "Invalid timestamp",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/vehicle/11111111111111111/version?timestamp=99-99-99", nil),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"Timestamp yyyymmdddate ","error":"Bad Request"}`,
		},
		{
			Name:             "Good request no timestamp",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/vehicle/11111111111111111/version", nil),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: `{"DBC":"dbc-version","TREX":"trex-version"}`,
		},
		{
			Name:             "Good request with timestamp",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/vehicle/11111111111111111/version?timestamp=2023-01-30", nil),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: `{"DBC":"dbc-version","TREX":"trex-version"}`,
		},
	}

	th.RunParamHttpTests(t, tests, handlers.HandleVersionsGet, "/vehicle/:vin/version")
}
