package mongo_test

import (
	"testing"

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

func TestNewFleetsCollection(t *testing.T) {
	client, err := mongo.NewClient(mongo.StandardDB)
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestNewFleetsCollection", nil, err)
		return
	}

	mongo.NewFleetsCollection(mongo.NewCollection(client.Collection("fleets")))
}

func TestFleetsAddFleet(t *testing.T) {
	collection := mongo.FleetsCollection{
		&mongo.MockCollection{},
	}

	err := collection.AddFleet(&mongo.Fleet{
		Name:     "TEST-FLEET",
		LogLevel: 2,
		CANBus: common.CANBus{
			Enabled:      true,
			DataLogger:   true,
			DTCEnabled:   elptr.ElPtr(true),
			MemBuffSize:  1 * 1024 * 1024,
			DiskBuffSize: 10 * 1024 * 1024,
		},
	})
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestFleetsAddFleet", nil, err)
	}
}

func TestFleetsFindFleet(t *testing.T) {
	collection := mongo.FleetsCollection{
		&mongo.MockCollection{},
	}

	_, err := collection.FindFleet(&mongo.Fleet{Name: "TEST-FLEET"})
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestFleetsFindFleet", nil, err)
	}
}

func TestFleetsUpdateFleet(t *testing.T) {
	collection := mongo.FleetsCollection{
		&mongo.MockCollection{},
	}

	fleet := mongo.Fleet{Name: "TEST-FlEET", CANBus: common.CANBus{Enabled: true, DataLogger: true, DTCEnabled: elptr.ElPtr(true)}, DebugMask: "12", IDPSEnabled: true}
	err := collection.UpdateFleet(&mongo.Fleet{Name: "TEST-FLEET"}, &fleet)
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestFleetsUpdateFleet", nil, err)
	}
}

func TestFleetsDeleteFleet(t *testing.T) {
	collection := mongo.FleetsCollection{
		&mongo.MockCollection{},
	}

	err := collection.AddFleet(&mongo.Fleet{Name: "TEST-FLEET"})
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestFleetsDeleteFleet", nil, err)
	}
}

func TestFleetsSelectFleets(t *testing.T) {
	collection := mongo.FleetsCollection{
		&mongo.MockCollection{},
	}

	_, err := collection.SelectFleets(&mongo.Fleet{Name: "TEST-FLEET"}, &queries.PageQueryOptions{Offset: 1, Limit: 1})
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestFleetsSelectFleets", nil, err)
	}
}

func TestFleetsGetFleetCount(t *testing.T) {
	collection := mongo.FleetsCollection{
		&mongo.MockCollection{},
	}

	_, err := collection.GetFleetCount(&mongo.Fleet{Name: "TEST-FLEET"})
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestFleetsGetFleetCount", nil, err)
	}
}

func TestFleetsGetVehiclesForFleet(t *testing.T) {
	collection := mongo.FleetsCollection{
		&mongo.MockCollection{},
	}

	_, err := collection.GetVehiclesForFleet("TEST-FLEET", "", &queries.PageQueryOptions{Offset: 1, Limit: 1})
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestFleetsGetVehiclesForFleet", nil, err)
	}
}

func TestFleetsGetVehiclesForFleetCount(t *testing.T) {
	collection := mongo.FleetsCollection{
		&mongo.MockCollection{
			AggregateObject: []struct{ Total int64 }{{1}},
		},
	}

	_, err := collection.GetVehiclesForFleetCount("TEST-FLEET", "")
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestFleetsGetVehiclesForFleetCount", nil, err)
	}
}

func TestFleetsAddVehiclesToFleet(t *testing.T) {
	collection := mongo.FleetsCollection{
		&mongo.MockCollection{},
	}

	err := collection.AddVehiclesToFleet("TEST-FLEET", []string{"TESTVIN123"})
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestFleetsAddVehiclesToFleet", nil, err)
	}
}

func TestFleetsRemoveVehicleFromFleet(t *testing.T) {
	collection := mongo.FleetsCollection{
		&mongo.MockCollection{},
	}

	err := collection.DeleteVehiclesFromFleet("TEST-FLEET", []string{"TESTVIN123"})
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestFleetsRemoveVehicleFromFleet", nil, err)
	}
}

func TestFleetsGetFiltersForFleet(t *testing.T) {
	collection := mongo.FleetsCollection{
		&mongo.MockCollection{},
	}

	_, err := collection.GetFiltersForFleet("TEST-FLEET", &queries.PageQueryOptions{Offset: 1, Limit: 1})
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestFleetsGetFiltersForFleet", nil, err)
	}
}

func TestFleetsAddFilterToFleet(t *testing.T) {
	collection := mongo.FleetsCollection{
		&mongo.MockCollection{},
	}

	err := collection.AddFilterToFleet("TEST-FLEET",
		&common.CANFilter{CANID: "123", Interval: elptr.ElPtr(456)})
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestFleetsAddFilterToFleet", nil, err)
	}
}

func TestFleetsUpdateFilterForFleet(t *testing.T) {
	collection := mongo.FleetsCollection{
		&mongo.MockCollection{},
	}

	err := collection.UpdateFilterForFleet("TEST-FLEET", "123",
		&common.CANFilter{CANID: "123", Interval: elptr.ElPtr(789)})
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestFleetsUpdateFilterForFleet", nil, err)
	}
}

func TestFleetsDeleteFilterForleet(t *testing.T) {
	collection := mongo.FleetsCollection{
		&mongo.MockCollection{},
	}

	err := collection.DeleteFilterFromFleet("TEST-FLEET", "123")
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestFleetsDeleteFilterForleet", nil, err)
	}
}

func TestFleetsGetFiltersForVehicle(t *testing.T) {
	collection := mongo.FleetsCollection{
		&mongo.MockCollection{},
	}

	_, err := collection.GetCANBusForVehicle("TESTVIN123")
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestFleetsGetFiltersForVehicle", nil, err)
	}
}
