package handlers_test

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

	"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 TestHandleFlashpackVersionInfoGet(t *testing.T) {
	mock := setupMockCars()
	services.GetDB().SetCars(setupMockCars())

	tests := []mo.DBHttpTest{
		{
			Name:             "Get info",
			Request:          th.MakeTestRequest(http.MethodGet, "/flashpack_version_info/11111111111111111", nil),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: `{"data":{"flashpack":"39.14","next_flashpack":"41.14","ecu_versions":[{"car_ecu_name":"ACUN","car_ecu_version":"ACUNVersion"}]}}`,
		},
	}

	mo.RunParamHttpTests(t, tests, handlers.HandleFlashpackVersionInfoGet, "/flashpack_version_info/:vin", mock)
}

func setupMockCars() *mo.MockCars {
	return &mo.MockCars{
		SelectResponse:    &common.Car{VIN: "11111111111111111", ICCID: "1111111111111111111F", Flashpack: "39.14", Model: "Ocean", Trim: "Base"},
		SelectCarSettings: []common.CarSetting{},
		SelectCarFlashpackVersions: []common.CarFlashpackVersion{

			// 46.14

			{
				CarModel:      "Ocean",
				CarTrim:       "Base",
				CarYear:       2023,
				Flashpack:     "46.14",
				CarECUName:    "ADAS",
				CarECUVersion: "ADASVersion2",
			},
			{
				CarModel:      "Ocean",
				CarTrim:       "Base",
				CarYear:       2023,
				Flashpack:     "46.14",
				CarECUName:    "ACUN",
				CarECUVersion: "ACUNVersionA",
			},
			{
				CarModel:      "Ocean",
				CarTrim:       "Base",
				CarYear:       2023,
				Flashpack:     "46.14",
				CarECUName:    "ACUN",
				CarECUVersion: "ACUNVersionB",
			},
			{
				CarModel:      "Ocean",
				CarTrim:       "Base",
				CarYear:       2023,
				Flashpack:     "46.14",
				CarECUName:    "BCM",
				CarECUVersion: "BCMVersion",
			},
			{
				CarModel:      "Ocean",
				CarTrim:       "Base",
				CarYear:       2023,
				Flashpack:     "46.14",
				CarECUName:    "PDI",
				CarECUVersion: "PDIVersion",
			},

			// 44.14

			{
				CarModel:      "Ocean",
				CarTrim:       "Base",
				CarYear:       2023,
				Flashpack:     "44.14",
				CarECUName:    "ADAS",
				CarECUVersion: "ADASVersion1",
			},
			{
				CarModel:      "Ocean",
				CarTrim:       "Base",
				CarYear:       2023,
				Flashpack:     "44.14",
				CarECUName:    "ACUN",
				CarECUVersion: "ACUNVersionA",
			},
			{
				CarModel:      "Ocean",
				CarTrim:       "Base",
				CarYear:       2023,
				Flashpack:     "44.14",
				CarECUName:    "ACUN",
				CarECUVersion: "ACUNVersionB",
			},
			{
				CarModel:      "Ocean",
				CarTrim:       "Base",
				CarYear:       2023,
				Flashpack:     "44.14",
				CarECUName:    "BCM",
				CarECUVersion: "BCMVersion",
			},
			{
				CarModel:      "Ocean",
				CarTrim:       "Base",
				CarYear:       2023,
				Flashpack:     "44.14",
				CarECUName:    "PDI",
				CarECUVersion: "PDIVersion",
			},

			// 41.14

			{
				CarModel:      "Ocean",
				CarTrim:       "Base",
				CarYear:       2023,
				Flashpack:     "41.14",
				CarECUName:    "ADAS",
				CarECUVersion: "ADASVersion",
			},
			{
				CarModel:      "Ocean",
				CarTrim:       "Base",
				CarYear:       2023,
				Flashpack:     "41.14",
				CarECUName:    "ACUN",
				CarECUVersion: "ACUNVersion",
			},
			{
				CarModel:      "Ocean",
				CarTrim:       "Base",
				CarYear:       2023,
				Flashpack:     "41.14",
				CarECUName:    "BCM",
				CarECUVersion: "BCMVersion",
			},
			{
				CarModel:      "Ocean",
				CarTrim:       "Base",
				CarYear:       2023,
				Flashpack:     "41.14",
				CarECUName:    "PDI",
				CarECUVersion: "PDIVersion",
			},

			// 39.14

			{
				CarModel:      "Ocean",
				CarTrim:       "Base",
				CarYear:       2023,
				Flashpack:     "39.14",
				CarECUName:    "ADAS",
				CarECUVersion: "ADASVersion0",
			},
			{
				CarModel:      "Ocean",
				CarTrim:       "Base",
				CarYear:       2023,
				Flashpack:     "39.14",
				CarECUName:    "ACUN",
				CarECUVersion: "ACUNVersion0",
			},
			{
				CarModel:      "Ocean",
				CarTrim:       "Base",
				CarYear:       2023,
				Flashpack:     "39.14",
				CarECUName:    "PDI",
				CarECUVersion: "PDIVersion",
			},

			// 37.14

			{
				CarModel:      "Ocean",
				CarTrim:       "Base",
				CarYear:       2023,
				Flashpack:     "37.14",
				CarECUName:    "PDI",
				CarECUVersion: "PDIVersion",
			},
		},
		SelectCarECUs: []common.CarECU{
			{
				VIN:               "11111111111111111",
				ECU:               "ADAS",
				SupplierSWVersion: "ADASVersion1",
			},
			{
				VIN:               "11111111111111111",
				ECU:               "ACUN",
				SupplierSWVersion: "ACUNVersion0",
			},
			{
				VIN:               "11111111111111111",
				ECU:               "BCM",
				SupplierSWVersion: "BCMVersion",
			},
			{
				VIN:               "11111111111111111",
				ECU:               "PDI",
				SupplierSWVersion: "PDIVersion",
			},
		},
	}
}
