package infrastructure

import (
	"context"
	"fmt"

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

// Repository definition.
type Repository struct {
	db     *mongo.Database
	logger *zap.Logger
}

// NewRepository create a new Repository instance.
func NewRepository(db *mongo.Database, logger *zap.Logger) *Repository {
	return &Repository{db: db,
		logger: logger.With(zap.String("module", "InfraestructureRepository")),
	}
}

// GetMongoStatus get mongo server status.
func (r *Repository) GetMongoStatus(ctx context.Context) (*MongoStatus, error) {
	command := bson.D{{Key: "serverStatus", Value: 1}}
	result := r.db.RunCommand(ctx, command)
	if result.Err() != nil {
		requestID := fmt.Sprintf("%v", ctx.Value("requestid"))
		r.logger.Error("failed execute command mongo serverStatus",
			zap.Error(result.Err()), zap.String("requestID", requestID))
		return nil, errors.WithStack(result.Err())
	}

	var mongoStatus MongoStatus
	err := result.Decode(&mongoStatus)
	if err != nil {
		requestID := fmt.Sprintf("%v", ctx.Value("requestid"))
		r.logger.Error("failed decoding cursor to *MongoStatus", zap.Error(err),
			zap.String("requestID", requestID))
		return nil, errors.WithStack(err)
	}
	return &mongoStatus, nil
}
