#!/usr/bin/python3

import sys
import pymongo as pym
# A module that formats the output in a readable format, which is quite useful
# in case of JSON documents.
import pprint 

def get_connection(mode):
  # If you have credentials, we refer to the documentation for more details:
  # https://pymongo.readthedocs.io/en/stable/examples/authentication.html
  
  if mode == "vm" or mode == "installation":
    return pym.MongoClient("mongodb://localhost")
  elif mode == "infrastructure":
    h = "studentdbserver01.dbresearch.plus.ac.at"
    p = 27017
    user = "username" # replace with your username
    pw = "password" # replace with your password
    mechanism = "PLAIN"
    source = "$external"

    return pym.MongoClient(
      host=h,
      port=p,
      username=user,
      password=pw,
      authMechanism=mechanism,
      authSource=source
    )
  elif mode == "docker":
    # Connection to local MongoDB database with credentials and using a specific authMechanism
    h = "localhost"
    p = 27017
    user = "dbtutorial"
    pw = "dbpwd1"
    source = "assignment2"

    return pym.MongoClient(
      host=h,
      port=p,
      username=user,
      password=pw,
      authSource=source
    )
  else:
    raise Exception("Unknown mode {} specified. Choose one of the following modes: vm, installation, infrastructure, docker.".format(mode))

def main():
  try:
    # Use mode "vm" if you are using the virtual machine (VM)
    # connection = get_connection("vm")
    
    # Use mode "installation" if you installed MongoDB natively
    # connection = get_connection("installation")
    
    # Use mode "infrastructure" if you are using our server infrastructure
    connection = get_connection("infrastructure")

    # Use mode "docker" if you are using Docker
    # connection = get_connection("docker")
  except Exception as e:
    print("Unable to connect to MongoDB database: {}.".format(e))
    sys.exit(1)
  
  try:
    # Use "ss2026-username" (and your actual username) if you are using our server infrastructure, e.g., ss2026-dkocher
    db = connection["assignment2"]

    # Create a dict (aka JSON object) for the find() operation
    json_query = {
      "author": "Michael Stonebraker",
      "booktitle": "VLDB"
    }

    # Execute the find() operation and retrieve a cursor to the result set
    cursor = db.dblp.find(json_query)
  
    # Print the documents in the result set to the command line. We use the
    # pprint module to print the JSON document in a human-readable format (the
    # standard print function prints the JSON documents in a single line). It is
    # up to you whether you want to use pprint or not.
    for i, x in enumerate(cursor):
      pprint.pprint({i: x})
  except Exception as e:
    print("Unable to execute simple find() query: {}".format(e))
  finally: # The finally-branch is executed independently of an exception.
    if cursor is not None:
      # Close the cursor.
      cursor.close()

    if connection is not None:
      # Close the connection.
      connection.close()

if __name__ == "__main__":
  main()