package handlers_test

import (
	"net/http"
	"testing"

	"otaupdate/handlers"
	"otaupdate/services"

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

func TestFleetVehicleAdd(t *testing.T) {
	client, err := services.GetMongoClient()
	if err != nil {
		t.Error(err)
		return
	}
	client.SetFleets(mongo.NewFleetsCollection(&mongo.MockCollection{}))
	client.SetVehicles(mongo.NewVehiclesCollection(&mongo.MockCollection{}))

	mock := mocks.MockCars{SelectCarsResponse: []common.Car{{
		VIN: "1F15K3R45N1234567",
	}}}
	services.GetDB().SetCars(&mock)

	tests := []th.BasicHttpTest{
		{
			Name:             "Invalid fleet",
			Request:          th.MakeTestRequest(http.MethodPost, "http://example.com/fleet/$TEST/vehicles/add", nil),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"primary key required","error":"Bad Request"}`,
		},
		{
			Name:             "Invalid data",
			Request:          th.MakeTestRequest(http.MethodPost, "http://example.com/fleet/US-TEST/vehicles/add", nil),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"VIN required_without VINs. VINs required_without VIN","error":"Bad Request"}`,
		},
		{
			Name: "No VINs",
			Request: th.MakeTestRequest(
				http.MethodPost,
				"http://example.com/fleet/US-TEST/vehicles/add",
				handlers.FleetVehicleParams{},
			),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"VIN required_without VINs. VINs required_without VIN","error":"Bad Request"}`,
		},
		{
			Name: "Invalid VIN",
			Request: th.MakeTestRequest(
				http.MethodPost,
				"http://example.com/fleet/US-TEST/vehicles/add",
				handlers.FleetVehicleParams{
					VIN: "TESTVIN",
				},
			),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"VIN 'TESTVIN' invalid","error":"Bad Request"}`,
		},
		{
			Name: "Valid VIN",
			Request: th.MakeTestRequest(
				http.MethodPost,
				"http://example.com/fleet/US-TEST/vehicles/add",
				handlers.FleetVehicleParams{
					VIN: "1F15K3R45N1234567",
				},
			),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: `{"vin":"1F15K3R45N1234567","vins":["1F15K3R45N1234567"],"canbus":{"enabled":true,"data_logger_enabled":false,"dtc_enabled":true}}`,
		},
		{
			Name: "Invalid VINs",
			Request: th.MakeTestRequest(
				http.MethodPost,
				"http://example.com/fleet/US-TEST/vehicles/add",
				handlers.FleetVehicleParams{
					VINs: []string{"TESTVIN"},
				},
			),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"VINs[0] 'TESTVIN' invalid","error":"Bad Request"}`,
		},
		{
			Name: "Valid VINs",
			Request: th.MakeTestRequest(
				http.MethodPost,
				"http://example.com/fleet/US-TEST/vehicles/add",
				handlers.FleetVehicleParams{
					VINs: []string{"1F15K3R45N1234567"},
				},
			),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: `{"vin":"","vins":["1F15K3R45N1234567"],"canbus":{"enabled":true,"data_logger_enabled":false,"dtc_enabled":true}}`,
		},
		{
			Name: "Multiple Invalid VINs",
			Request: th.MakeTestRequest(
				http.MethodPost,
				"http://example.com/fleet/US-TEST/vehicles/add",
				handlers.FleetVehicleParams{
					VINs: []string{"TESTVIN", "TESTVIN2"},
				},
			),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"VINs[0] 'TESTVIN' invalid. VINs[1] 'TESTVIN2' invalid","error":"Bad Request"}`,
		},
		{
			Name: "Multiple Mixed Valid VINs",
			Request: th.MakeTestRequest(
				http.MethodPost,
				"http://example.com/fleet/US-TEST/vehicles/add",
				handlers.FleetVehicleParams{
					VINs: []string{"1F15K3R45N1234567", "TESTVIN"},
				},
			),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"VINs[1] 'TESTVIN' invalid","error":"Bad Request"}`,
		},
		{
			Name: "Multiple Valid VINs",
			Request: th.MakeTestRequest(
				http.MethodPost,
				"http://example.com/fleet/US-TEST/vehicles/add",
				handlers.FleetVehicleParams{
					VINs: []string{"1F15K3R45N1234567", "1F15K3R45N1234567"},
				},
			),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: `{"vin":"","vins":["1F15K3R45N1234567","1F15K3R45N1234567"],"canbus":{"enabled":true,"data_logger_enabled":false,"dtc_enabled":true}}`,
		},
		{
			Name: "Multiple Valid VINs",
			Request: th.MakeTestRequest(
				http.MethodPost,
				"http://example.com/fleet/US-TEST/vehicles/add",
				handlers.FleetVehicleParams{
					VIN:  "1F15K3R45N1234567",
					VINs: []string{"1F15K3R45N1234567"},
				},
			),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: `{"vin":"","vins":["1F15K3R45N1234567","1F15K3R45N1234567"],"canbus":{"enabled":true,"data_logger_enabled":false,"dtc_enabled":true}}`,
		},
	}

	th.RunParamHttpTests(t, tests, handlers.HandleFleetVehicleAdd, "/fleet/:name/vehicles/add")
}
