''' This script adds all data from BitYoga's csv to our pipeline.'''

import csv
import requests

# file to read the data from
CSV_FILE = r'Energy_Dataset.csv'

# token from Rest Gateway to authorize 
JWT_TOKEN = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6InJlZ3VsYXJAaXRlYy5hYXUuYXQiLCJjcmVhdGVkX2F0IjoiMjAyMC0xMC0wNiAwOTowNzoyMy44MjEyOTciLCJ2YWxpZF91bnRpbCI6IjIwMjAtMTAtMDcgMDk6MDc6MjMuODIxMjk3In0.orqTjn-3J4qMM0kpreWVPkwUEtUcgoqo6wsUFxKCrwg'


def send_transaction_to_rest_gateway(transaction: dict):
    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
    )

    print(res)


if __name__ == '__main__':    
    with open(CSV_FILE, 'r') as file:
        reader = csv.reader(file)

        titles = next(reader)
        
        summ = {}          

        for row in reader:
            transaction = {}
            transaction['ApplicationType'] = 'smart-energy-luca'
            transaction['docType'] = 'smart-energy-luca'
            for idx in range(len(row)):
                transaction[titles[idx]] = row[idx]

            # if int(transaction['Customer']) < 176:      
            #     # only upload until 200 for now
            #     continue

            send_transaction_to_rest_gateway(transaction)

            # 17520 per customer
            cid = transaction["Customer"]
            if cid not in summ:
                summ[cid] = 0
            summ[cid] += 1

        sum2 = list(summ)
        sum2.sort()
        print([(k, summ[k]) for k in sum2])
        # print(sum([v for v in summ.values()]))
      