package mongo

import (
	"context"
	"encoding/json"

	"github.com/fiskerinc/cloud-services/pkg/db/queries"

	"github.com/pkg/errors"
	"go.mongodb.org/mongo-driver/mongo"
)

func NewMockClient() Client {
	c := &Conn{}
	c.SetVehicles(&VehiclesCollection{&MockCollection{}})
	c.SetFleets(&FleetsCollection{&MockCollection{}})
	return &mockClient{c}
}

type mockClient struct {
	*Conn
}

func (m *mockClient) Ping(ctx context.Context) error {
	return nil
}

// MockCollection is used as an embedded struct for mock tests
type MockCollection struct {
	AggregateObject interface{}
	FindObject      interface{}
}

func (m *MockCollection) InsertOne(document interface{}) (interface{}, error) {
	return nil, nil
}

func (m *MockCollection) FindOne(filter interface{}, object interface{}, projection interface{}) error {
	return nil
}

func (m *MockCollection) ReplaceOne(filter interface{}, update interface{}) error {
	return nil
}

func (m *MockCollection) DeleteOne(document interface{}) error {
	return nil
}

func (m *MockCollection) UpdateOne(filter interface{}, update interface{}) (*mongo.UpdateResult, error) {
	return &mongo.UpdateResult{
		MatchedCount:  0,
		ModifiedCount: 1,
		UpsertedCount: 0,
		UpsertedID:    []string{},
	}, nil
}

func (m *MockCollection) UpdateMany(filter interface{}, update interface{}) error {
	return nil
}

func (m *MockCollection) Find(filter interface{}, objects interface{}, options *queries.PageQueryOptions) error {
	data, err := json.Marshal(m.FindObject)
	if err != nil {
		return errors.WithStack(err)
	}

	err = json.Unmarshal(data, objects)
	if err != nil {
		return errors.WithStack(err)
	}

	return nil
}

func (m *MockCollection) Count(filter interface{}) (int64, error) {
	return 0, nil
}

func (m *MockCollection) Aggregate(pipeline mongo.Pipeline, object interface{}) error {
	data, err := json.Marshal(m.AggregateObject)
	if err != nil {
		return errors.WithStack(err)
	}

	err = json.Unmarshal(data, object)
	if err != nil {
		return errors.WithStack(err)
	}

	return nil
}

// A separate client where mock collection have bunch of methods overwritten
// specifically for certain unit tests like the one to validate DebugMask enablement.
func NewMockMongoClient() Client {
	c := &Conn{}
	m := newMockVehicleCollection()
	c.SetVehicles(&VehiclesCollection{&m})
	c.SetFleets(&FleetsCollection{&MockCollection{}})
	return &mockMongoClient{c}
}

type mockMongoClient struct {
	*Conn
}

// It carries a specific vehicle inserted for mocking.
type MockVehicleCollection struct {
	VehiclesCollection
	vehicle Vehicle
}

func newMockVehicleCollection() MockVehicleCollection {
	return MockVehicleCollection{}
}

func (m *MockVehicleCollection) InsertOne(document interface{}) (interface{}, error) {
	vh := document.(*Vehicle)
	m.vehicle = *vh
	return vh, nil
}

func (m *MockVehicleCollection) FindOne(filter interface{}, object interface{}, projection interface{}) error {
	argVh := object.(*Vehicle)
	argVh.VIN = m.vehicle.VIN
	argVh.DebugMask = m.vehicle.DebugMask
	return nil
}

func (m *MockVehicleCollection) FindVehicle(vf *Vehicle) (*Vehicle, error) {
	v := m.vehicle
	return &v, nil
}

func (m *MockVehicleCollection) GetVehicles() VehiclesCollectionInterface {
	return m
}
