package handlers_test

import (
	"net/http"
	"testing"

	"otaupdate/handlers"
	"otaupdate/services"

	"github.com/fiskerinc/cloud-services/pkg/mongo"
	th "github.com/fiskerinc/cloud-services/pkg/testhelper"
)

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

	tests := []th.BasicHttpTest{
		{
			Name: "Invalid fleet",
			Request: th.MakeTestRequest(
				http.MethodPost,
				"http://example.com/fleet/$TEST/vehicles/delete",
				handlers.FleetVehicleDeleteParams{
					Name: "$TEST", // only needed for test
					VINs: []string{"1F15K3R45N1234567"},
				},
			),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"primary key required","error":"Bad Request"}`,
		},
		{
			Name: "Invalid vin",
			Request: th.MakeTestRequest(
				http.MethodPost,
				"http://example.com/fleet/US-TEST/vehicles/delete",
				handlers.FleetVehicleDeleteParams{
					Name: "US-TEST", // only needed for test
					VINs: []string{"INVALIDVIN"},
				},
			),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"primary key required","error":"Bad Request"}`,
		},
		{
			Name: "Valid data",
			Request: th.MakeTestRequest(
				http.MethodPost,
				"http://example.com/fleet/US-TEST/vehicles/delete",
				handlers.FleetVehicleDeleteParams{
					Name: "US-TEST", // only needed for test
					VINs: []string{"1F15K3R45N1234567"},
				},
			),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: `{"message":"Deleted"}`,
		},
		{
			Name: "Multiple VINs",
			Request: th.MakeTestRequest(
				http.MethodPost,
				"http://example.com/fleet/US-TEST/vehicles/delete",
				handlers.FleetVehicleDeleteParams{
					Name: "US-TEST", // only needed for test
					VINs: []string{"1F15K3R45N1234567", "1F15K3R45N1234567"},
				},
			),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: `{"message":"Deleted"}`,
		},
	}

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