// Package mock provides mock implementations for testing
package mock

import (
	"context"
	"fmt"

	"github.com/pfeifferj/container-mom-go/pkg/operator/mongodb"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

// MockMongoDB implements a mock MongoDB client for testing
type MockMongoDB struct {
	deployments map[string]*mongodb.MongoDeployment
}

// NewMockMongoDB creates a new mock MongoDB client
func NewMockMongoDB() *MockMongoDB {
	return &MockMongoDB{
		deployments: make(map[string]*mongodb.MongoDeployment),
	}
}

func (m *MockMongoDB) GetDeployment(_ context.Context, id string) (*mongodb.MongoDeployment, error) {
	if deployment, ok := m.deployments[id]; ok {
		return deployment, nil
	}
	return nil, fmt.Errorf("deployment not found")
}

func (m *MockMongoDB) ListDeployments(_ context.Context) ([]mongodb.MongoDeployment, error) {
	deployments := make([]mongodb.MongoDeployment, 0, len(m.deployments))
	for _, d := range m.deployments {
		deployments = append(deployments, *d)
	}
	return deployments, nil
}

func (m *MockMongoDB) CreateDeployment(_ context.Context, deployment *mongodb.MongoDeployment) error {
	m.deployments[deployment.ID] = deployment
	return nil
}

func (m *MockMongoDB) UpdateDeployment(_ context.Context, deployment *mongodb.MongoDeployment) error {
	if _, ok := m.deployments[deployment.ID]; !ok {
		return fmt.Errorf("deployment not found")
	}
	m.deployments[deployment.ID] = deployment
	return nil
}

func (m *MockMongoDB) DeleteDeployment(_ context.Context, id string) error {
	delete(m.deployments, id)
	return nil
}

// Ping implements a mock ping
func (m *MockMongoDB) Ping(_ context.Context, _ interface{}) error {
	return nil
}

// GetClient returns nil for the mock
func (m *MockMongoDB) GetClient() *mongo.Client {
	// Return a mock client that satisfies the interface
	client, _ := mongo.NewClient(options.Client())
	return client
}

func (m *MockMongoDB) Disconnect(_ context.Context) error {
	return nil // Nothing to disconnect in mock
}

func (m *MockMongoDB) Database(name string) *mongo.Database {
	// Return a mock database
	client := m.GetClient()
	return client.Database(name)
}
