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 TestNewVehiclesCollection(t *testing.T) {
	client, err := mongo.NewClient(mongo.StandardDB)
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestNewVehiclesCollection", nil, err)
		return
	}

	mongo.NewVehiclesCollection(mongo.NewCollection(client.Collection("vehicles")))
}

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

	err := collection.AddVehicle(&mongo.Vehicle{VIN: "TESTVIN123", CANBus: common.CANBus{Enabled: true, DataLogger: true, DTCEnabled: elptr.ElPtr(true)}})
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestVehiclesAddVehicle", nil, err)
	}
}

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

	_, err := collection.FindVehicle(&mongo.Vehicle{VIN: "TESTVIN123"})
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestVehiclesFindVehicle", nil, err)
	}
}

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

	err := collection.AddVehicle(&mongo.Vehicle{VIN: "TESTVIN123", CANBus: common.CANBus{Enabled: true, DataLogger: true, DTCEnabled: elptr.ElPtr(false)}, DebugMask: "E"})
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestVehiclesUpdateVehicle", nil, err)
	}
}

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

	err := collection.DeleteVehicle(&mongo.Vehicle{VIN: "TESTVIN123"})
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestVehiclesDeleteVehicle", nil, err)
	}
}

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

	err := collection.AddFilterToVehicle("TESTVIN123",
		&common.CANFilter{CANID: "123", Interval: elptr.ElPtr(100)})
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestAddFilterToVehicle", nil, err)
	}
}

func TestVehiclesGetFiltersForVehicle(t *testing.T) {
	collection := mongo.VehiclesCollection{
		&mongo.MockCollection{
			AggregateObject: []mongo.Vehicle{{VIN: "TESTVIN123"}},
		},
	}

	_, err := collection.GetFiltersForVehicle("TESTVIN123", &queries.PageQueryOptions{Offset: 1, Limit: 1})
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestVehiclesGetFiltersForVehicle", nil, err)
	}
}

func TestVehiclesGetFleetsForVehicle(t *testing.T) {
	collection := mongo.VehiclesCollection{
		&mongo.MockCollection{
			AggregateObject: []struct {
				Fleets []string `bson:"fleets"`
			}{{Fleets: []string{"fleet1", "fleet2"}}}}}

	_, err := collection.GetFleetsForVehicle(
		"TESTVIN123", "", &queries.PageQueryOptions{Offset: 1, Limit: 1})
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestVehiclesGetFleetsForVehicle", nil, err)
	}
}

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

	_, err := collection.GetFleetCountForVehicle("TESTVIN123", "")
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestVehiclesGetFleetsCountForVehicle", nil, err)
	}
}
func TestVehiclesUpdateFilterForVehicle(t *testing.T) {
	collection := mongo.VehiclesCollection{
		&mongo.MockCollection{},
	}

	err := collection.UpdateFilterForVehicle("TESTVIN123", "123",
		&common.CANFilter{Interval: elptr.ElPtr(100)})
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestVehiclesUpdateFilterForVehicle", nil, err)
	}
}

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

	err := collection.DeleteFilterForVehicle("TESTVIN123", "123")
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestVehiclesDeleteFilterForVehicle", nil, err)
	}
}

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

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

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

	err := collection.UpdateFiltersForVehicle("TESTVIN123", []common.CANFilter{{CANID: "123",
		Interval: elptr.ElPtr(100)}})
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestVehiclesUpdateFiltersForVehicle", nil, err)
	}
}
