/*
 * Returns whether the provided pointers to users are in each other's friends lists
 *
 * Return:
 *   - 1 if they are in each other's friends lists.
 *   - 0 if they are not in each other's friends lists.
 */
int check_friends_helper(const User *first_user, const User *second_user) {
    // iterating through both friends lists to see if they are already friends
    int already_friends_index;
    for (already_friends_index = 0; already_friends_index < MAX_FRIENDS; already_friends_index++) {
        if ((strcmp(first_user->friends[already_friends_index]->name, second_user->name) == 0) ||
            (strcmp(second_user->friends[already_friends_index]->name, first_user->name) == 0)) {
            return 1; // return 1 if the users are already friends
        }
    }
    return 0;
}

/*
 * Returns the first empty index in the friends list of the user that passed in
 *
 * Return:
 *   - first empty index if such an index exists
 *   - -1 if the friends list has not empty spots
 */
int first_empty_friends_index(const User *user) {
    // iterating through both friends lists to see if they are already friends and if both friends have available spots
    // in their friends lists
    int friend_array_index;
    for (friend_array_index = 0; friend_array_index < MAX_FRIENDS; friend_array_index++) {
        if (user->friends[friend_array_index] == NULL) {
            return friend_array_index;
        }
    }
    return -1;
}

/*
 * Make two users friends with each other.  This is symmetric - a pointer to
 * each user must be stored in the 'friends' array of the other.
 *
 * New friends must be added in the first empty spot in the 'friends' array.
 *
 * Return:
 *   - 0 on success.
 *   - 1 if the two users are already friends.
 *   - 2 if the users are not already friends, but at least one already has
 *     MAX_FRIENDS friends.
 *   - 3 if the same user is passed in twice.
 *   - 4 if at least one user does not exist.
 *
 * Do not modify either user if the result is a failure.
 * NOTE: If multiple errors apply, return the *largest* error code that applies.
 */
int make_friends(const char *name1, const char *name2, User *head) {
    // points the user objects with the provided usernames if they exist, NULL otherwise
    User *first_user = find_user(name1, head);
    User *second_user = find_user(name2, head);

    if ((first_user == NULL) || (second_user == NULL)) {
        return 4; // if either user was not found through our search, return 4
    }

    if (strcmp(name1, name2) == 0) {
        return 3; // return 3 if the usernames are the same (same user was provided twice)
    }

    // the location of a free space in either user's friends list, set to -1 if no free spaces are available
    int first_user_empty_index = first_empty_friends_index(first_user);
    int second_user_empty_index = first_empty_friends_index(second_user);

    if (first_user_empty_index == -1 || second_user_empty_index == -1) {
        return 2; // if either index is -1, there are no empty places in one of their friends lists so return 2
    }

    if (check_friends_helper(first_user, second_user) == 1) {
        return 1; // return 1 if the users are already friends
    }

    // setting the values at the friends list indexes to the other user in each user object
    (first_user->friends)[first_user_empty_index] = second_user;
    (second_user->friends)[second_user_empty_index] = first_user;

    return 0;
}
add two user objects to each other's friends attribute which is a linked list