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

    # TODO disscuss with others if it looks ok

    use_case = "car-sharing"
    columns = [
        # "doctype",
        "id",
        "carLicensePlate",
        # "users",
        "offerId",
        # "startPlace",
        # "endPlace",
        "startDate",
        "endDate",
        # "suggestedEndPlaces",
        "rentForTime",
        "seats",
        "totalPrice",
        "observations",
        # "finishedBy",
        "kmTraveled",
        "priceBalance",
        "depositBalance",
        "rewardBalance",
        # "score",
        # "startedBy",
        # "checkedBy",
        "status",
        # "cancelledBy"
    ]
    
    columns = { c : c for c in columns }
    columns["UniqueID"] = "id"
    columns["user1ID"] = "users[0]//userId"
    columns["user1PassengersNr"] = "users[0]//passengers"
    columns["isDriver"] = "users[0]//isDriver"
    columns["startPlaceLatitude"] = "startPlace//latitude"
    columns["startPlaceLongitude"] = "startPlace//longitude"
    columns["endPlaceLatitude"] = "endPlace//latitude"
    columns["endPlaceLongitude"] = "endPlace//longitude"
    #TODO consider only the first sugested place? how to consider multiple
    #columns["suggestedEndPlace1Latitude"] = "suggestedEndPlaces[0]//endplace//latitude"
    #columns["suggestedEndPlace1Longitude"] = "suggestedEndPlaces[0]//endplace//longitude"
    #columns["finishedBy1User"] = "finishedBy[0]//user"
    #columns["finishedBy1Moment"] = "finishedBy[0]//moment"
    #columns["finishedBy1Latitude"] = "finishedBy[0]//coordinate//latitude"
    #columns["finishedBy1Longitude"] = "finishedBy[0]//coordinate//longitude"
    #columns["score1User"] = "score[0]//user"
    #columns["score1Rating"] = "score[0]//value"
    columns["startedBy1StartPlaceLatitude"] = "startedBy[0]//startPlace//latitude"
    columns["startedBy1StartPlaceLongitude"] = "startedBy[0]//startPlace//longitude"
    columns["startedBy1Moment"] = "startedBy[0]//moment"
    columns["startedBy1User"] = "startedBy[0]//user"
    #TODO CheckedBy,CanceledBy usefull?

    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": "TravelUser_Layer",
            "properties": [
                'UniqueID',
                'user1ID',                
                'isDriver',
                #'score1User',
                #'score1Rating',
                'startedBy1User'
            ],
            "cluster_properties": [
                'user1ID'
            ]
        },
        {
            "use_case": use_case,
            "table": table_name,
            "name": "StartingPoint_Layer",
            "properties": [
                'user1ID',
                'UniqueID',
                'startPlaceLatitude',
                'startPlaceLongitude',
                'startDate',
                'endDate'
            ],
            "cluster_properties": [
                'startPlaceLatitude',
                'startPlaceLatitude'
            ]
        },{
            "use_case": use_case,
            "table": table_name,
            "name": "StartingTime_Layer",
            "properties": [
                'user1ID',
                'UniqueID',
                'startDate',
                'endDate',
                #'finishedBy1Moment',
                'startedBy1Moment'
            ],
            "cluster_properties": [
                'startedBy1Moment',
                'startDate'
            ]
        },{
            "use_case": use_case,
            "table": table_name,
            "name": "Destination_Layer", 
            "properties": [
                'user1ID',
                'UniqueID',
                #'finishedBy1Latitude',
                #'finishedBy1Longitude',
                'kmTraveled',
                'startPlaceLatitude',
                'startPlaceLongitude',
            ],
            "cluster_properties": [
                #'finishedBy1Latitude',
                #'finishedBy1Longitude'
            ]
        },{
            "use_case": use_case,
            "table": table_name,
            "name": "FinishedTime_Layer", 
            "properties": [
                'user1ID',
                'UniqueID',
                'startDate',
                'endDate',
                #'finishedBy1Moment',
                'startedBy1Moment'
            ],
            "cluster_properties": [
                'endDate' #,
                #'finishedBy1Moment'
            ]
        },{
            "use_case": use_case,
            "table": table_name,
            "name": "Price_Layer",
            "properties": [
                'user1ID',
                'UniqueID',
                'totalPrice',
                'kmTraveled',
                'priceBalance',
                'depositBalance',
                'rewardBalance'
            ],
            "cluster_properties": [
                'totalPrice',
                'kmTraveled',
                'priceBalance',
                'depositBalance',
                'rewardBalance'
            ]
        },
        {
            "use_case": use_case,
            "table": table_name,
            "name": "Status_Layer",
            "properties": [
                'UniqueID',
                'user1ID',                
                'status',
                #'score1User',
                #'score1Rating',
                'rentForTime'
            ],
            "cluster_properties": [
                'status',
                #'score1Rating',
                'rentForTime'
            ]
        }
    ]

    postLayersToSwagger(use_case,layers)

def main(use_case: str):
    print("TRAVEL")
    table_name = "travel"
    add_table(use_case,table_name)
    add_layers(use_case, table_name)
