import requests
import json
import numpy as np
import pandas as pd
from pathlib import Path
import Apikeys
import os
import ts_lstdict

BASEFILE = "/Users/normrasmussen/Downloads/ts-completions.csv"
API_KEY = Apikeys.TALKSPACE_1099
UUID_URL = "https://api.northpass.com/v2/people?filter[email][eq]="
PROP_URL = "https://api.northpass.com/v2/properties/people/"
HEADERS = {
    "accept": "*/*",
    "x-api-key": API_KEY,
    "content-type": "application/json",
}
LISTDICT = ts_lstdict.LISTDICT


def load_file():
    dict_list = []
    row_dict = {}
    file = Path(BASEFILE)
    completions = pd.read_csv(file)
    try:
        if os.path.isfile(file):
            print(f"File found! Importing {file}")
            for email in completions.itertuples():
                row_num = email[0]
                name = email[1]
                email = email[3]
                row_dict = {
                    "row_num": row_num,
                    "name": name,
                    "userid": "",
                    "email": email,
                }
                dict_list.append(row_dict)
            print(len(dict_list))
            tempdf = pd.DataFrame(dict_list)
            tempdf.drop_duplicates(subset=["name"], keep="first", inplace=True)
            dict_list = tempdf.to_dict("records")
            print(len(dict_list))
    except KeyError as e:
        print(f"{e} Error")
    finally:
        make_calls(dict_list, completions)


def make_calls(dict_list, completions):
    final_list = []
    for row_dict in dict_list:
        name = row_dict["name"]
        url = UUID_URL + f"{row_dict['email']}"
        response = requests.get(url, headers=HEADERS)
        print(f"running api call for {name}")
        if response.status_code == 200:
            response = response.json()
            try:
                uuid = response["data"][0]["id"]
                url2 = PROP_URL + f"{uuid}"
                response = requests.get(url2, headers=HEADERS)
                try:
                    data = response.json()
                    userid = data["data"]["attributes"]["properties"]["user_id"]
                    row_dict["userid"] = userid
                except Exception:
                    pass
                finally:
                    pass
            except IndexError as e:
                print(f"{e} Second error!")
                pass
            finally:
                pass
            if row_dict["userid"] is None:
                pass
            else:
                final_list.append(row_dict)
            print(len(final_list))
    add_to_csv(final_list, completions)


def add_to_csv(final_list, completions):
    # completions = pd.read_csv(BASEFILE)
    completions["user_id"] = ""
    for info in final_list:
        row = info["name"]
        email = info["email"]
        pid = info["userid"]
        completions.loc[completions["email"] == email, "user_id"] = pid
        # completions['User ID'] = np.where(completions['email'] == email, pid, None)
    print(completions)
    completions.to_csv(
        "/Users/normrasmussen/Downloads/Talkspace1099-Completions-All-IDS.csv",
    )


if __name__ == "__main__":
    load_file()
    # add_to_csv()
