import pymongo
import pandas as pd
import numpy as np
import csv
import sys

print("Read arguments from the terminal:")
for i in sys.argv[1:]:
    print(i)
    
project_data_path=sys.argv[1]

print("\n\nStarting the connection to the database...")
client = pymongo.MongoClient('localhost', 27017)

print("\nExisting databases: {a}".format(a=client.list_database_names()))
db = client.customers

print("\nExisting collections in 'customers': {a}".format(a=db.list_collection_names()))
colle = db.deploy_data

print("\nExisting documents in 'deploy_data': {a}".format(a=colle.count_documents({})))

print("\nStoring data into a data frame...")
datos=pd.DataFrame(list(colle.find()))

print("\nClosing the connection to database...")
client.close()

print("\nFiltering data by 'natural person'...")
print("\nRows before filtering: {a}".format(a=datos.shape[0]))
datos = datos[datos["person"]=="natural"]
print("\nRows after filtering: {a}".format(a=datos.shape[0]))

print("\nCreating dummy variables...")
datos["Spain"]=np.where(datos["Geography"]=="Spain",1,0)
datos["Germany"]=np.where(datos["Geography"]=="Germany",1,0)
datos["Female"]=np.where(datos["Gender"]=="Female",1,0)

print("\nRecalling names of train features...")
X_train_names=list()
with open(project_data_path+"X_names.csv", 'r') as csvfile:
    foo = csv.reader(csvfile)
    for _ in foo:
        X_train_names.extend(_)
csvfile.close()

print("\nSelecting X_deploy columns...")
X_deploy=datos[X_train_names]
print("\nShape of the data now: {a}".format(a=datos.shape))
print(X_deploy.head())

print("\nExtracting X_deploy_ids...")
X_deploy_ids = datos[["CustomerId","Name","person"]]
print(X_deploy_ids.head())

print("\nExporting the data...")
for fname in ['X_deploy', 'X_deploy_ids']:
    with open(project_data_path+fname+".csv", 'w', newline='') as csvfile:
        writer = csv.writer(csvfile)
        row_list = (eval(fname)).values.tolist()
        writer.writerows(row_list)
        csvfile.close()
print("\nClose connection to file: {a}".format(a=csvfile.closed))

print("\nDone!")