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 check back with alex

    use_case = "car-sharing"
    columns = [
        # "docType",
        "id",
        "priceForKm",
        "priceForTime",
        "startDate",
        "endDate",
        "deposit",
        # "startPlace",
        # "endPlaces",
        "available",
        "deleted",
        # "cancelledBy",
        "carOwner"
        # "checkedBy",
        # "finishedBy"
    ]
    
    columns = { c : c for c in columns }
    columns["UniqueID"] = "id"
    columns["carID"] = "car//carLicensePlate"
    columns["carOwner"] = "car//ownerId"
    ##TODO Question: should i get all the data from the car field? or just the car id/owner?
    columns["startLatitude"] = "startPlace//latitude"
    columns["startLongitude"] = "startPlace//longitude"
    #TODO what if there are more end places? how can i define them? i can only get the first index of the array
    columns["endFirstLatitude"] = "endPlaces[0]//latitude"
    columns["endFirstLongitude"] = "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": "OfferStartLocation_layer",
            "properties": [
                "UniqueID",
                "carID",
                "carOwner", 
                "startLatitude",
                "startLongitude",
                "startDate",
                "endFirstLatitude",
                "endFirstLongitude",

            ],
            "cluster_properties": [
                "carOwner",
                "startLatitude",
                "startLongitude"
            ]
        },
        {
            "use_case": use_case,
            "table": table_name,
            "name": "OfferEndLocation_layer",
            "properties": [
                "UniqueID",
                "carID",
                "carOwner", 
                "startLatitude",
                "startLongitude",
                "endDate",
                "endFirstLatitude",
                "endFirstLongitude",

            ],
            "cluster_properties": [
                "carOwner",
                "endFirstLatitude",
                "endFirstLongitude"
            ]
        },
        {
            "use_case": use_case,
            "table": table_name,
            "name": "OfferedCar_layer",
            "properties": [
                "UniqueID",
                "carID",
                "carOwner"

            ],
            "cluster_properties": [
                "carID",
                "carOwner"
            ]
        },
        {
            "use_case": use_case,
            "table": table_name,
            "name": "OfferPrice_layer",
            "properties": [
                "UniqueID",
                "carOwner",
                "priceForKm",
                "priceForTime",
                "deposit"

            ],
            "cluster_properties": [
                "carOwner",
                "priceForKm",
                "priceForTime",
                "deposit"
            ]
        },
        {
            "use_case": use_case,
            "table": table_name,
            "name": "OfferTime_layer",
            "properties": [
                "UniqueID",
                "carOwner",
                "startDate",
                "endDate"

            ],
            "cluster_properties": [
                "carOwner",
                "startDate",
                "endDate"
            ]
        },
        {
            "use_case": use_case,
            "table": table_name,
            "name": "OfferStatus_layer",
            "properties": [
                "UniqueID",
                "carOwner",
                "available",
                "deleted"

            ],
            "cluster_properties": [
                "carOwner",
                "available",
                "deleted"
            ]
        }
    ]

    postLayersToSwagger(use_case,layers)

def main(use_case: str):
    print("OFFER")
    table_name = "offer"
    add_table(use_case,table_name)
    add_layers(use_case,table_name)