package usecase_helpers

import (
	"testing"

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

var someErr = errors.New("some err")

func TestPopulateECUsCurrentVersion(t *testing.T) {
	mockVIN := ""
	tests := map[string]struct {
		cars    queries.CarsInterface
		ecus    []*m.UpdateManifestECU
		expEcus []*m.UpdateManifestECU
		expErr  error
	}{
		"correct": {
			cars: &mocks.MockCars{
				SelectCarECUs: []m.CarECU{
					{ECU: "TREX", Version: " S213C213"},
					{ECU: "PKC", Version: " S213C222"},
				},
			},
			ecus: []*m.UpdateManifestECU{
				{ECU: "TREX"},
				{ECU: "PKC"},
				{ECU: "MPT"},
			},
			expEcus: []*m.UpdateManifestECU{
				{ECU: "TREX", CurrentVersion: " S213C213"},
				{ECU: "PKC", CurrentVersion: " S213C222"},
				{ECU: "MPT"},
			},
		},
		"empty_car_ecus": {
			cars: &mocks.MockCars{},
			ecus: []*m.UpdateManifestECU{
				{ECU: "TREX"},
				{ECU: "PKC"},
				{ECU: "MPT"},
			},
			expEcus: []*m.UpdateManifestECU{
				{ECU: "TREX"},
				{ECU: "PKC"},
				{ECU: "MPT"},
			},
		},
		"get_car_ecus_error": {
			cars: &mocks.MockCars{
				DBMockHelper: mocks.DBMockHelper{Error: someErr},
			},
			ecus: []*m.UpdateManifestECU{
				{ECU: "TREX"},
				{ECU: "PKC"},
				{ECU: "MPT"},
			},
			expErr: someErr,
		},
	}

	for tname, tt := range tests {
		t.Run(tname, func(t *testing.T) {
			err := PopulateECUsCurrentVersion(tt.cars, mockVIN, tt.ecus)
			if err != nil && tt.expErr != nil {
				assert.Equal(t, tt.expErr.Error(), err.Error())
				return
			}
			assert.Equal(t, tt.expErr, err)
			assert.Equal(t, tt.expEcus, tt.ecus)
		})
	}
}
