''' This script adds all data from BitYoga's csv to our pipeline.'''
import csv
import requests
import sys
import os
import json

import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

# modules_path = '../../../modules/'
# if os.path.exists(modules_path):
#     sys.path.insert(1, modules_path)

# import network_constants as nc
# from security.token_manager import TokenManager


def send_transaction_to_rest_gateway(transaction: dict):
    # token from Rest Gateway to authorize 
    JWT_TOKEN = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6InJlZ3VsYXJAaXRlYy5hYXUuYXQiLCJjcmVhdGVkX2F0IjoiMjAyMS0wMy0xNiAxMzoxNDoyMS42MDc1NjciLCJ2YWxpZF91bnRpbCI6IjIwMjEtMDMtMTcgMTM6MTQ6MjEuNjA3NTY3In0.ZGObriEDWYo1BgiYN3pQSosS7UuNrq10GSCSjmRHSAw'

    res = requests.post(
        url = 'https://articonf1.itec.aau.at:30401/api/trace',
        json = transaction,
        headers = {"Authorization": f"Bearer {JWT_TOKEN}"},
        verify = False # ignore ssl error
    )

    if res.status_code >= 400:
        raise Exception(f"Error while uploading: {str(res.content)}")

    #print(res) Lots of spam

# file to read the data from
JSON_DATASET = r'reddit_dataset.json'
if __name__ == '__main__':    
    with open(JSON_DATASET, 'r') as json_file:
        # reader = csv.reader(file)
        json_data_list = json.load(json_file)
        #titles = next(json_data)
        
        summ = 0          

        for obj_dict in json_data_list:
            transaction = {}
            transaction['ApplicationType'] = 'reddit'
            transaction['docType'] = 'reddit'

            for key, value in obj_dict.items():
                transaction[key] = value

            send_transaction_to_rest_gateway(transaction)
            summ+=1

            if (summ % 1000 == 0 ):
                print ("Uploaded " + str(summ) + " transactions.")
            if summ >= 1:
                break
        print ("TOTAL Uploaded " + str(summ) + " transactions.")
      