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 = [
        "Customer", 
        "Postcode", 
        "Timestamp", 
        "Solar_Production_kWh", 
        "Energy_Consumption_kWh", 
        "Heating_Consumption_kWh", 
        "Price_AUD/MWh", 
        "Total_Demand_MWh", 
        "Latitude", 
        "Longitude",
        "User_Demand_kWh",
    ]
    
    columns = { c.replace("/", "_") : c for c in columns }
    columns["UniqueID"] = "Customer+Postcode+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": "Solar_Production_Layer",
            "properties": [
                "UniqueID",
                "Customer",
                "Postcode",
                "Timestamp",
                "Solar_Production_kWh"
            ],
            "cluster_properties": [
                "Solar_Production_kWh"
            ]
        },
        {
            "use_case": use_case,
            "table": table_name,
            "name": "Energy_Consumption_Layer",
            "properties": [
                "UniqueID",
                "Customer",
                "Postcode",
                "Timestamp",
                "Energy_Consumption_kWh"
            ],
            "cluster_properties": [
                "Energy_Consumption_kWh"
            ]
        },{
            "use_case": use_case,
            "table": table_name,
            "name": "Heating_Consumption_Layer",
            "properties": [
                "UniqueID",
                "Customer",
                "Postcode",
                "Timestamp",
                "Heating_Consumption_kWh"
            ],
            "cluster_properties": [
                "Heating_Consumption_kWh"
            ]
        },{
            "use_case": use_case,
            "table": table_name,
            "name": "Price_Layer",
            "properties": [
                "UniqueID",
                "Customer",
                "Postcode",
                "Timestamp",
                "Price_AUD_MWh"
            ],
            "cluster_properties": [
                "Price_AUD_MWh"
            ]
        },{
            "use_case": use_case,
            "table": table_name,
            "name": "Demand_Layer",
            "properties": [
                "UniqueID",
                "Customer",
                "Postcode",
                "Timestamp",
                "Total_Demand_MWh"
            ],
            "cluster_properties": [
                "Total_Demand_MWh"
            ]
        },{
            "use_case": use_case,
            "table": table_name,
            "name": "Location_Layer",
            "properties": [
                "UniqueID",
                "Customer",
                "Postcode",
                "Timestamp",
                "Latitude", 
                "Longitude"
            ],
            "cluster_properties": [
                "Latitude",
                "Longitude",
            ]
        },
        {
            "use_case": use_case,
            "table": table_name,
            "name": "User_Demand_Layer",
            "properties": [
                "UniqueID",
                "Customer",
                "Postcode",
                "Timestamp",
                "User_Demand_kWh"
            ],
            "cluster_properties": [
                "User_Demand_kWh",
            ]
        },
    ]

    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: str = "smart-energy", table_name: str = "smart-energy"):
    print("SMART-ENERGY")

    add_table(use_case, table_name)
    add_layers(use_case, table_name)