# add modules folder to interpreter path
import sys
import os
modules_path = '../modules/'
if os.path.exists(modules_path):
    sys.path.insert(1, modules_path)

import pymongo
import network_constants

MONGO_DB_HOST = network_constants.HOST_IP
MONGO_DB_PORT = '30003'

class MongoRepository:
    # TODO extract to docker env var
    _username = 'root'
    _password = 'root'
    _collection : pymongo.collection.Collection = None

    def __init__(self, username=_username, password=_password):
        myclient = pymongo.MongoClient(f"mongodb://{username}:{password}@{MONGO_DB_HOST}:{MONGO_DB_PORT}/")
        database = myclient['traceRetrievalDB'] # trace retrieval
        self._collection = database['traces']

    def insert_trace(self, content: dict):
        self._collection.insert_one(content)

    def get_traces(self, selection: dict = {}):
        return self._collection.find(selection)

    def drop_collection(self):
        self._collection.drop()

traces = MongoRepository().get_traces()
for t in traces:
    print(str(t))

# MongoRepository().drop_collection()