package mongo_test

import (
	"errors"
	"testing"

	e "github.com/fiskerinc/cloud-services/pkg/mongo/error"

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

	"go.mongodb.org/mongo-driver/bson"
	"github.com/fiskerinc/cloud-services/pkg/utils/elptr"
)

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

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

func TestCollectionInsertOneIntegration(t *testing.T) {
	t.Skip()

	client, err := mongo.NewClient(mongo.StandardDB)
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestCollectionInsertOneIntegration", nil, err)
		return
	}
	collection := mongo.NewCollection(client.Collection("vehicles"))

	v := mongo.Vehicle{
		VIN: "TESTVIN123",
	}
	_, err = collection.InsertOne(v)
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestCollectionInsertOneIntegration", nil, err)
	}

	f := mongo.Fleet{
		Name: "testfleet",
	}
	_, err = collection.InsertOne(f)
	if !errors.Is(err, e.ErrInvalidStruct) {
		t.Errorf(testhelper.TestErrorTemplate, "TestCollectionInsertOneIntegration", e.ErrInvalidStruct, err)
	}
}

func TestCollectionFindOneIntegration(t *testing.T) {
	t.Skip()

	client, err := mongo.NewClient(mongo.StandardDB)
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestCollectionFindOneIntegration", nil, err)
		return
	}
	collection := mongo.NewCollection(client.Collection("vehicles"))

	var vehicle *mongo.Vehicle
	vin := "TESTVIN123"
	err = collection.FindOne(vin, vehicle, nil)
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestCollectionFindOneIntegration", nil, err)
	}

	vin = "TESTVIN456"
	err = collection.FindOne(vin, vehicle, nil)
	if err == nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestCollectionFindOneIntegration", "error", err)
	}
}

func TestCollectionReplaceOneIntegration(t *testing.T) {
	t.Skip()

	client, err := mongo.NewClient(mongo.StandardDB)
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestCollectionReplaceOneIntegration", nil, err)
		return
	}
	collection := mongo.NewCollection(client.Collection("vehicles"))

	f := bson.M{"vin": "TESTVIN123"}
	u := bson.M{"$set": bson.M{"fleets": bson.A{}}}
	_, err = collection.UpdateOne(f, u)
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestCollectionReplaceOneIntegration", nil, err)
	}

	f = bson.M{"vin": "TESTVIN456"}
	u = bson.M{"$set": bson.M{"fleets": bson.A{}}}
	_, err = collection.UpdateOne(f, u)
	if err == nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestCollectionReplaceOneIntegration", "error", err)
	}
}

func TestCollectionDeleteOneIntegration(t *testing.T) {
	t.Skip()

	client, err := mongo.NewClient(mongo.StandardDB)
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestCollectionDeleteOneIntegration", nil, err)
		return
	}
	collection := mongo.NewCollection(client.Collection("vehicles"))

	f := bson.M{"vin": "TESTVIN123"}
	err = collection.DeleteOne(f)
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestCollectionDeleteOneIntegration", nil, err)
	}

	f = bson.M{"vin": "TESTVIN456"}
	err = collection.DeleteOne(f)
	if err == nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestCollectionDeleteOneIntegration", "error", err)
	}
}

func TestCollectionUpdateOneIntegration(t *testing.T) {
	t.Skip()

	client, err := mongo.NewClient(mongo.StandardDB)
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestCollectionUpdateOneIntegration", nil, err)
		return
	}
	collection := mongo.NewCollection(client.Collection("vehicles"))

	m := bson.M{"vin": "TESTVIN123"}
	u := bson.M{"$addToSet": bson.M{"canbus.filters": []common.CANFilter{{CANID: "123", Interval: elptr.ElPtr(100)}}}}
	_, err = collection.UpdateOne(m, u)
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestCollectionUpdateOneIntegration", nil, err)
	}
}

func TestCollectionUpdateManyIntegration(t *testing.T) {
	t.Skip()

	client, err := mongo.NewClient(mongo.StandardDB)
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestCollectionUpdateManyIntegration", nil, err)
		return
	}
	collection := mongo.NewCollection(client.Collection("vehicles"))

	m := bson.M{}
	u := bson.M{"$addToSet": bson.M{"canbus.filters": []common.CANFilter{{CANID: "123", Interval: elptr.ElPtr(100)}}}}
	err = collection.UpdateMany(m, u)
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestCollectionUpdateManyIntegration", nil, err)
	}
}

func TestCollectionFindIntegration(t *testing.T) {
	t.Skip()

	client, err := mongo.NewClient(mongo.StandardDB)
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestCollectionFindIntegration", nil, err)
		return
	}
	collection := mongo.NewCollection(client.Collection("vehicles"))

	m := bson.M{"vin": "TESTVIN123"}
	vehicles := []mongo.Vehicle{}
	err = collection.Find(m, &vehicles, nil)
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestCollectionFindIntegration", nil, err)
	}
}

func TestCollectionCountIntegration(t *testing.T) {
	t.Skip()

	client, err := mongo.NewClient(mongo.StandardDB)
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestCollectionCountIntegration", nil, err)
		return
	}
	collection := mongo.NewCollection(client.Collection("vehicles"))

	m := bson.M{}
	_, err = collection.Count(m)
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestCollectionCountIntegration", nil, err)
	}
}

func TestCollectionAggregateIntegration(t *testing.T) {
	t.Skip()

	client, err := mongo.NewClient(mongo.StandardDB)
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestCollectionAggregateIntegration", nil, err)
		return
	}
	collection := mongo.NewCollection(client.Collection("vehicles"))

	pipeline := []bson.D{}
	vehicles := []mongo.Vehicle{}
	err = collection.Aggregate(pipeline, &vehicles)
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestCollectionAggregateIntegration", nil, err)
	}
}
