import csv
import hashlib

import sys
import os
modules_paths = ['.', '../../../modules/']
for modules_path in modules_paths:
    if os.path.exists(modules_path):
        sys.path.insert(1, modules_path)

from messaging.MessageHandler import MessageHandler

# file to read the data from
CSV_FILE = r'dummy_upload/bank_app/bank_data.csv'
handler = MessageHandler()


def upload_transaction(transaction):
    uid = hashlib.sha256(f"{transaction['Transaction_ID']}{transaction['Timestamp']}".encode("utf-8")).hexdigest()
    transaction['UniqueID'] = uid

    transaction['ApplicationType'] = 'bank-app'
    transaction['docType'] = 'bank-app'

    t = {        
        'use_case': transaction['ApplicationType'],
        'table': transaction['docType'],
        'id': uid,
        'properties': transaction,
    }
    handler.handle_new_trace(t)


type_mapping = { 'House Rent': 1, 'Payback Loan': 2, 'Initial Credit': 3, 'Emergency Help': 4, 'Friendly Help': 5 }


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

        titles = next(reader)

        for row in reader:
            transaction = {}
            for idx in range(len(row)):
                transaction[titles[idx]] = row[idx]

            transaction['Transaction_Type'] = type_mapping[transaction['Transaction_Type']]

            upload_transaction(transaction)

