from typing import OrderedDict


def manipulate_df(df, user_id, questionnaire_id, collected_at, created_at, updated_at, keep_answer_timestamps=False):
    """
    Reads the elements of the answers and creates keys and values from the answers in a format fit for appending to the
    final dataframe
    :param df:
    :param user_id:
    :param questionnaire_id:
    :return: A dictionary that becomes the row of the dataframe
    """

    a = OrderedDict()
    a['user_id'] = user_id
    a['questionnaire_id'] = questionnaire_id
    a['created_at'] = created_at
    if keep_answer_timestamps:
        a['collected_at'] = collected_at
        a['updated_at'] = updated_at
        for row in df.itertuples():
            time_label = row.label + '_collected_at'
            a[time_label] = row.collected_at
            a[row.label] = row.value

    else:
        for row in df.itertuples():
            #time_label = row.label + '_collected_at'
            #a[time_label] = row.collected_at
            a[row.label] = row.value

    return a