from pathlib import Path
import requests
import pandas as pd
import itertools

ndir = Path("/Users/normrasmussen/Documents/Work/CustomerNotes/")


def get_files():
    merge = pd.DataFrame()
    dict_copy = pd.DataFrame()
    empty_temp = pd.DataFrame()
    files = list(ndir.glob("**/*.md"))
    for company in files:
        # This Section gets the Company name for the payload.
        company = str(company)
        company_file = company.split("/")[-1]
        company_name = company_file[:-3]

        list_of_dicts = get_data(company_file, company_name)
        # This returns a list of dictionaries - one list per company

        dic = pd.DataFrame.from_records(list_of_dicts)
        # This gets overwritten for each list out put by the function.
        # So once a list comes out, it needs to be copied.

        dict_copy = dic.copy()
        # Now that it is copied.. merge it here first?

        empty_temp = pd.concat([dict_copy, empty_temp], ignore_index=True)
    merge = pd.concat([empty_temp, merge], ignore_index=True)
    merge.to_csv("/Users/normrasmussen/Downloads/Notes_for_GS.csv")
    print(merge)


def get_data(company_file, company_name):
    notes = []
    line_num_array = []
    text_array = []
    list_of_dicts = []
    pay_dict = {"company_name": company_name}
    company_path = Path(
        f"/Users/normrasmussen/Documents/Work/CustomerNotes/{company_file}"
    )
    with company_path.open(mode="r", encoding="utf-8") as md_file:
        line = company_path.read_text()[3]

        # This first section finds the lines and adds them to an array.
        # It also finds the "date" lines and adds them to a second array.
        for num, line in enumerate(md_file):
            line = line[:-1]
            text_array.append(line)
            if line.startswith("## "):
                line_num_array.append(num)

        # Since I don't know the last line of the file, I take the last
        # number in the title array and adds 500,
        # going well beyond the last line.
        last_line = (line_num_array[-1], 500)
        line_num_array.append(sum(last_line))

        # This magic pairs each title based on the line numbers.
        line_pairs = list(itertools.pairwise(line_num_array))

        # We take each "grouping" and add it to an array as each singular item.
        for index in line_pairs:
            indv_notes = text_array[index[0] : index[1]]
            notes.append(indv_notes)

        # Now we are cleaning it up.
        # Removing the hashes from the dates and titles.
        for note in notes:
            date = note[0][3:]
            copy = " ".join(note[1:])
            pay_dict["date"] = date
            pay_dict["copy"] = copy
            pay_dict["type"] = "Meeting"
            if note[2].startswith("### "):
                note_title = note.pop(2)[4:]
                pay_dict["title"] = note_title
            else:
                pay_dict["title"] = f"Meeting Notes from {date}"
            list_of_dicts.append(pay_dict.copy())
    # print(list_of_dicts)
    return list_of_dicts


def add_to_payload(pay_dict):
    payload = {
        "records": [
            {
                "Author": "nrasmussen@gainsight.com",
                "ContextName": pay_dict["company_name"],
                "TypeName": "Meeting",
                "ExternalId": "WHAT IS THIS?",
                "Subject": pay_dict["title"],
                "Notes": pay_dict["copy"],
                "ActivityDate": pay_dict["date"],
                "companyName": "AAR Corp Hardware Abscoa Division",
                "internalAttendees": ["Norm Rasmussen"],
                "externalAttendees": [""],
            },
        ]
    }
    print(payload)


"""
    # send_to_gs(payload)


def send_to_gs(payload):
    url = "https://gongnxt.gainsightcloud.com/v1/ant/es/activity/bulk"
    json = payload
    headers = {
        "Content Type": "JSON",
        "Accesskey": APIKEY,
    }
    response = requests.post(url=url, headers=headers, json=payload)
"""

if __name__ == "__main__":
    get_files()
