package handlers_test

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

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

func TestHandleFlashpackVersionAdd(t *testing.T) {
	// mock := mo.MockCars{}
	// services.GetDB().SetCars(&mock)

	tests := []mo.DBHttpTest{
		{
			Name: "Bad data",
			Request: th.MakeTestRequest(http.MethodPost, "http://example.com/flashpack_version", m.CarFlashpackVersionAddRequest{
				ECUVersions: []m.ECUVersionRequest{
					{
						CarECUName:    "ADAS",
						CarECUVersion: "ADASVersion",
					},
					{
						CarECUName:    "RV",
						CarECUVersion: "RVVersion",
					},
				},
				Flashpack: "41.14",
				CarModel:  "Ocean",
				CarTrim:   "Base",
			}),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"CarYear required","error":"Bad Request"}`,
		},
		{
			Name: "Bad data no ECUs",
			Request: th.MakeTestRequest(http.MethodPost, "http://example.com/flashpack_version", m.CarFlashpackVersionAddRequest{
				ECUVersions: []m.ECUVersionRequest{},
				Flashpack:   "41.14",
				CarModel:    "Ocean",
				CarTrim:     "Base",
				CarYear:     2023,
			}),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"CarECUName and CarECUVersion required","error":"Bad Request"}`,
		},
		{
			Name: "Valid data",
			Request: th.MakeTestRequest(http.MethodPost, "http://example.com/flashpack_version", m.CarFlashpackVersionAddRequest{
				ECUVersions: []m.ECUVersionRequest{
					{
						CarECUName:    "ADAS",
						CarECUVersion: "ADASVersion5",
					},
					{
						CarECUName:    "RV",
						CarECUVersion: "RVVersion",
					},
				},
				Flashpack: "11.14",
				CarModel:  "Ocean",
				CarTrim:   "Base",
				CarYear:   2025,
			}),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: `{"message":"Created"}`,
		},
	}

	mo.RunParamHttpTests(t, tests, handlers.HandleFlashpackVersionAdd, "/flashpack_version", nil)
}
