package flashpackversion_test

import (
	"testing"

	"github.com/fiskerinc/cloud-services/pkg/common"
	"github.com/fiskerinc/cloud-services/pkg/db/queries/mocks"
	fv "github.com/fiskerinc/cloud-services/pkg/flashpackversion"
	"github.com/stretchr/testify/assert"
)

func TestFindCurrentFlashpackVersionForCar(t *testing.T) {
	mockCars := setupMockCars()

	car := common.Car{
		VIN:   "FISKER123",
		Model: "Ocean",
		Trim:  "Base",
		Year:  2023,
	}

	mockCars.SelectCarECUs = []common.CarECU{
		{
			VIN:               "FISKER123",
			ECU:               "ADAS",
			SupplierSWVersion: "ADASVersion2",
		},
		{
			VIN:               "FISKER123",
			ECU:               "ACUN",
			SupplierSWVersion: "ACUNVersionA",
		},
		{
			VIN:               "FISKER123",
			ECU:               "BCM",
			SupplierSWVersion: "BCMVersion",
		},
		{
			VIN:               "FISKER123",
			ECU:               "PDI",
			SupplierSWVersion: "PDIVersion",
		},
		{
			VIN:               "FISKER123",
			ECU:               "OBC",
			SupplierSWVersion: "PDUVersion",
		},
		{
			VIN:               "FISKER123",
			ECU:               "MCU",
			SupplierSWVersion: "TBOXVersion",
		},
	}

	flashpack, err := fv.FindCurrentFlashpackVersionForCar(&mockCars, car)

	assert.Nil(t, err)
	assert.Equal(t, "46.14", flashpack)

	mockCars.SelectCarECUs = []common.CarECU{
		{
			VIN:               "FISKER123",
			ECU:               "ADAS",
			SupplierSWVersion: "ADASVersion",
		},
		{
			VIN:               "FISKER123",
			ECU:               "ACUN",
			SupplierSWVersion: "ACUNVersion",
		},
		{
			VIN:               "FISKER123",
			ECU:               "BCM",
			SupplierSWVersion: "BCMVersion",
		},
	}

	flashpack, err = fv.FindCurrentFlashpackVersionForCar(&mockCars, car)

	assert.Nil(t, err)
	assert.Equal(t, "", flashpack)

	mockCars.SelectCarECUs = []common.CarECU{
		{
			VIN:               "FISKER123",
			ECU:               "ADAS",
			SupplierSWVersion: "ADASVersion1",
		},
		{
			VIN:               "FISKER123",
			ECU:               "ACUN",
			SupplierSWVersion: "ACUNVersion",
		},
		{
			VIN:               "FISKER123",
			ECU:               "BCM",
			SupplierSWVersion: "BCMVersion",
		},
		{
			VIN:               "FISKER123",
			ECU:               "PDI",
			SupplierSWVersion: "PDIVersion",
		},
	}

	flashpack, err = fv.FindCurrentFlashpackVersionForCar(&mockCars, car)

	assert.Nil(t, err)
	assert.Equal(t, "41.14", flashpack)

	mockCars.SelectCarECUs = []common.CarECU{
		{
			VIN:               "FISKER123",
			ECU:               "ADAS",
			SupplierSWVersion: "ADASVersion1",
		},
		{
			VIN:               "FISKER123",
			ECU:               "ACUN",
			SupplierSWVersion: "ACUNVersion0",
		},
		{
			VIN:               "FISKER123",
			ECU:               "BCM",
			SupplierSWVersion: "BCMVersion",
		},
		{
			VIN:               "FISKER123",
			ECU:               "PDI",
			SupplierSWVersion: "PDIVersion",
		},
	}

	flashpack, err = fv.FindCurrentFlashpackVersionForCar(&mockCars, car)

	assert.Nil(t, err)
	assert.Equal(t, "39.14", flashpack)

	mockCars.SelectCarECUs = []common.CarECU{
		{
			VIN:               "FISKER123",
			ECU:               "PDI",
			SupplierSWVersion: "PDIVersion",
		},
	}

	flashpack, err = fv.FindCurrentFlashpackVersionForCar(&mockCars, car)

	assert.Nil(t, err)
	assert.Equal(t, "37.14", flashpack)
}

func TestFindCarECUsToUpdateForNextFlashpackNumber(t *testing.T) {
	mockCars := setupMockCars()

	car := common.Car{
		VIN:   "FISKER123",
		Model: "Ocean",
		Trim:  "Base",
		Year:  2023,
	}

	mockCars.SelectCarECUs = []common.CarECU{
		{
			VIN:               "FISKER123",
			ECU:               "ADAS",
			SupplierSWVersion: "ADASVersion0",
		},
		{
			VIN:               "FISKER123",
			ECU:               "ACUN",
			SupplierSWVersion: "ACUNVersion0",
		},
		{
			VIN:               "FISKER123",
			ECU:               "PDI",
			SupplierSWVersion: "PDIVersion",
		},
	}

	ecus, err := fv.FindCarECUsToUpdateForNextFlashpackNumber(&mockCars, car, "41.14")

	assert.Nil(t, err)
	assert.Equal(t, []common.CarECUVersion{
		{
			CarECUName:    "ADAS",
			CarECUVersion: "ADASVersion",
		},
		{
			CarECUName:    "ACUN",
			CarECUVersion: "ACUNVersion",
		},
		{
			CarECUName:    "BCM",
			CarECUVersion: "BCMVersion",
		},
	}, ecus)

	mockCars.SelectCarECUs = []common.CarECU{
		{
			VIN:               "FISKER123",
			ECU:               "ADAS",
			SupplierSWVersion: "ADASVersion",
		},
		{
			VIN:               "FISKER123",
			ECU:               "ACUN",
			SupplierSWVersion: "ACUNVersion",
		},
		{
			VIN:               "FISKER123",
			ECU:               "PDI",
			SupplierSWVersion: "PDIVersion",
		},
		{
			VIN:               "FISKER123",
			ECU:               "BCM",
			SupplierSWVersion: "BCMVersion",
		},
		{
			VIN:               "FISKER123",
			ECU:               "OBC",
			SupplierSWVersion: "PDUVersion",
		},
	}

	ecus, err = fv.FindCarECUsToUpdateForNextFlashpackNumber(&mockCars, car, "44.14")

	assert.Nil(t, err)
	assert.Equal(t, []common.CarECUVersion{
		{
			CarECUName:    "ADAS",
			CarECUVersion: "ADASVersion1",
		},
		{
			CarECUName:    "ACUN",
			CarECUVersion: "ACUNVersionA",
		},
		{
			CarECUName:    "ACUN",
			CarECUVersion: "ACUNVersionB",
		},
	}, ecus)
}

func setupMockCars() mocks.MockCars {
	return mocks.MockCars{
		SelectResponse:    &common.Car{VIN: "FISKER123", ICCID: "1111111111111111111F"},
		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",
			},
			{
				CarModel:      "Ocean",
				CarTrim:       "Base",
				CarYear:       2023,
				Flashpack:     "46.14",
				CarECUName:    "PDU",
				CarECUVersion: "PDUVersion",
			},
			{
				CarModel:      "Ocean",
				CarTrim:       "Base",
				CarYear:       2023,
				Flashpack:     "46.14",
				CarECUName:    "TBOX",
				CarECUVersion: "TBOXVersion",
			},

			// 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",
			},
			{
				CarModel:      "Ocean",
				CarTrim:       "Base",
				CarYear:       2023,
				Flashpack:     "44.14",
				CarECUName:    "PDU",
				CarECUVersion: "PDUVersion",
			},

			// 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",
			},
		},
	}
}
