import network_constants as nc
from security.token_manager import TokenManager

import requests

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 = {}


    use_case = "reddit"
    columns = [
        # "doctype",
        "id",
        "user_id",
        "title",
        "content",
        "permalink",
        "upvotes",
        "percentage_upvoted",
        "n_comments",
        "subreddit",
        "created_at"
    ]
    
    columns = { c : c for c in columns }
    
    columns["UniqueID"] = "user_id+subreddit+id"

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

    url = f"https://articonf1.itec.aau.at:30420/api/use-cases/{use_case}/tables"
    jwt = TokenManager.getInstance().getToken()
    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 add_layers(use_case:str, table_name: str):
    layers = [
        # {  #subreddit is string cannot cluster
        #     "use_case": use_case,
        #     "table": table_name,
        #     "name": "Subreddit_Layer",
        #     "properties": [
        #         "UniqueID",
        #         "subreddit",
        #         "user_id",
        #         "title",
        #         "content",
        #         "permalink",
        #         "upvotes",
        #         "percentage_upvoted",
        #         "n_comments"
        #     ],
        #     "cluster_properties": [
        #         "subreddit"
        #     ]
        # },
        {
            "use_case": use_case,
            "table": table_name,
            "name": "Upvotes_Layer", #TODO Probably do something like Total Votes? so we can get a popularity?
            "properties": [
                "UniqueID",
                "subreddit",
                "user_id",
                "title",
                "content",
                "permalink",
                "upvotes",
                "percentage_upvoted",
                "n_comments",
                "created_at"
            ],
            "cluster_properties": [
                "upvotes",
                "percentage_upvoted"
            ]
        },
        {
            "use_case": use_case,
            "table": table_name,
            "name": "Percentage_Layer",
            "properties": [
                "UniqueID",
                "subreddit",
                "user_id",
                "title",
                "content",
                "permalink",
                "upvotes",
                "percentage_upvoted",
                "n_comments",
                "created_at"
            ],
            "cluster_properties": [
                "percentage_upvoted"
            ]
        },
        {
            "use_case": use_case,
            "table": table_name,
            "name": "Engagement_Layer",
            "properties": [
                "UniqueID",
                "subreddit",
                "user_id",
                "title",
                "content",
                "permalink",
                "upvotes",
                "percentage_upvoted",
                "n_comments",
                "created_at"
            ],
            "cluster_properties": [
                "n_comments"
            ]
        },
        {
            "use_case": use_case,
            "table": table_name,
            "name": "Time_Layer",
            "properties": [
                "UniqueID",
                "subreddit",
                "user_id",
                "title",
                "content",
                "permalink",
                "upvotes",
                "percentage_upvoted",
                "n_comments",
                "created_at"
            ],
            "cluster_properties": [
                "created_at"  
            ]
        }
    ]

    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))    

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