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

    use_case = "car-sharing"
    columns = [
        # "doctype",
        "id",
        "text",
        #"media"
        #"location"
        "user",
        "date",
        #"likesUsers"
        #"reportedBy"
        "deleted",
        "answerTo"
    ]
    
    columns = { c : c for c in columns }
    columns["UniqueID"] = "id"
    #TODO what if it doesn't have media? i.e the list is empty
    columns["mediaUrlFirst"] = "media[0]//mediaUrl"
    columns["mediaTypeFirst"]= "media[0]//mediaType"
    columns["mediaLabelsFirst"]= "media[0]//mediaLabels"
    columns["latitude"] = "location//latitude"
    columns["longitude"] = "location//longitude"
    #TODO get Likes/reportedBy?

    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": "PublicationLocation_layer",
            "properties": [
                "UniqueID",
                "user",
                "latitude",
                "longitude"
                
            ],
            "cluster_properties": [
                "latitude",
                "longitude"  
            ]
        },
        {
            "use_case": use_case,
            "table": table_name,
            "name": "PublicationTime_layer",
            "properties": [
                "UniqueID",
                "user",
                "latitude",
                "longitude"
                
            ],
            "cluster_properties": [
                "date"
            ]
        },
        {
            "use_case": use_case,
            "table": table_name,
            "name": "PublicationUser_layer",
            "properties": [
                "UniqueID",
                "text",
                "user",
                "mediaUrlFirst",
                "mediaTypeFirst",
                "mediaLabelsFirst"
                
            ],
            "cluster_properties": [
                "user"
            ]
        },
        {
            "use_case": use_case,
            "table": table_name,
            "name": "PublicationStatus_layer",
            "properties": [
                "UniqueID",
                "text",
                "user",
                "deleted",
                "answerTo"
                
            ],
            "cluster_properties": [
                "deleted",
                "answerTo"
            ]
        }
    ]

    postLayersToSwagger(use_case,layers)

def main(use_case: str):
    print("PUBLICATION")
    table_name = "publication"
    add_table(use_case,table_name)
    add_layers(use_case, table_name)