from _add_use_case_scripts.requestPost import postLayersToSwagger, postTableToSwagger

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 "_"
    '''

    
    columns = [
        # None
    ]
    #TODO unique id is dependent on offferId -> error?
    columns = { c : c for c in columns }
    columns["UniqueID"] = "offerId+endPlacesLatitude+endPlacesLongitude"
    columns["offerId"] = "offer//id"
    columns["endPlacesAddress"]= "endPlaces[0]//address",
    columns["endPlacesLatitude"]= "endPlaces[0]//latitude",
    columns["endPlacesLongitude"]= "endPlaces[0]//longitude"

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

    postTableToSwagger(use_case,table)

def add_layers(use_case:str, table_name: str):
    layers = [
        {
            "use_case": use_case,
            "table": table_name,
            "name": "OfferEndPlace_layer",
            "properties": [
                "UniqueID",
                "offerID", 
                "endPlacesAddress",
                "endPlacesLatitude",
                "endPlacesLongitude"
            ],
            "cluster_properties": [
                "endPlacesLatitude",
                "endPlacesLongitude"
            ]
        },
        {
            "use_case": use_case,
            "table": table_name,
            "name": "OfferEndAddress_layer",
            "properties": [
                "UniqueID",
                "offerID", 
                "endPlacesAddress",
                "endPlacesLatitude",
                "endPlacesLongitude"
            ],
            "cluster_properties": [
                "endPlacesAddress"
            ]
        }        
    ]

    postLayersToSwagger(use_case,layers)

def main(use_case: str):
    print("OFFER_ENDPLACES")
    table_name = "offerEndPlaces"
    add_table(use_case,table_name)
    add_layers(use_case,table_name)