import network_constants as nc



from security.token_manager import TokenManager

from typing import Dict, List
import requests

def add_layers(use_case:str, table_name: str, layers: List):
    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)+" "+response.text)

def add_table(use_case: str, table_name: str, columns: Dict):
    jwt = TokenManager.getInstance().getToken()
    
    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 main(use_case: str):
    print("PIZZA")

    # table 1
    add_table(
        use_case, 
        "order", 
        {
            "UniqueID": "orderId",
            "pizzaId": "pizza",
            "totalPrice": "price"
        }
    )

    # table 2
    add_table(
        use_case, 
        "pizza", 
        {
            "UniqueID": "id",
            "name": "name",
            "doughType": "dough//type",
            "hasSpinach": "dough//spinach",
            "firstTopping": "toppings[0]//name",
            "firstToppingPrice": "toppings[0]//price",
            "toppingInfo": "toppings[0]//name+toppings[0]//price"
        }
    )

    # layers table 1
    add_layers(use_case, "pizza", 
        [
            {
                "use_case": use_case,
                "table": "pizza",
                "name": "Price_Layer",
                "properties": [
                    "UniqueID",
                    "firstToppingPrice"
                ],
                "cluster_properties": [
                    "firstToppingPrice"
                ]
            },
            {
                "use_case": use_case,
                "table": "pizza",
                "name": "Dough_Layer",
                "properties": [
                    "UniqueID",
                    "doughType"
                ],
                "cluster_properties": [
                    "doughType"
                ]
            },
        ]
    )