import network_constants as nc
from security.token_manager import TokenManager

import requests

def add_table(use_case: str, table_name: str):
    '''
    take the columns and add the mappings at the server
    replace all "/"'s in the internal representation with a "_"
    '''
    jwt = TokenManager.getInstance().getToken()
    
    columns = [
        "Transaction_ID", 
        "From_User_ID", 
        "To_User_ID", 
        "Timestamp", 
        "Amount", 
        "Transaction_Type", 
    ]
    
    columns = { c : c for c in columns }
    columns["UniqueID"] = "Transaction_ID+Timestamp"
    
    url = f"https://articonf1.itec.aau.at:30420/api/use-cases/{use_case}/tables"

    table = {
        "name": table_name,
        "mappings": columns
    }

    response = requests.post(
            url,
            verify=False, 
            proxies = { "http":None, "https":None },
            headers = { "Authorization": f"Bearer {jwt}"},
            json = table
        )

    print(url+": "+str(response.status_code))

def add_layers(use_case: str, table_name: str):
    layers = [
        {
            "use_case": use_case,
            "table": table_name,
            "name": "Amount_Layer",
            "properties": [
                "UniqueID",
                "From_User_ID",
                "To_User_ID",
                "Timestamp",
                "Amount"
            ],
            "cluster_properties": [
                "Amount"
            ]
        },
        {
            "use_case": use_case,
            "table": table_name,
            "name": "Transaction_Type_Layer",
            "properties": [
                "UniqueID",
                "From_User_ID",
                "To_User_ID",
                "Timestamp",
                "Transaction_Type"
            ],
            "cluster_properties": [
                "Transaction_Type"
            ]
        },
    ]

    jwt = TokenManager.getInstance().getToken()

    for layer in layers:
        url = f"https://articonf1.itec.aau.at:30420/api/layers"

        response = requests.post(
            url,
            verify=False, 
            proxies = { "http":None, "https":None },
            headers = { "Authorization": f"Bearer {jwt}"},
            json = layer
        )

        print(url+": "+str(response.status_code))

def main(use_case_name):
    table_name = 'bank-app'

    add_table(use_case_name, table_name)
    add_layers(use_case_name, table_name)