#include <time.h>

#define MAX_NAME 32     // Max username and profile_pic filename lengths
#define MAX_FRIENDS 10  // Max number of friends a user can have

typedef struct user {
    char name[MAX_NAME];
    char profile_pic[MAX_NAME];  // This is a *filename*, not the file contents.
    struct post *first_post;
    struct user *friends[MAX_FRIENDS];
    struct user *next;
} User;

typedef struct post {
    char author[MAX_NAME];
    char *contents;
    time_t *date;
    struct post *next;
} Post;

int make_post(const User *author, User *target, char *contents) {
    if (author == NULL || target == NULL) {
        return 2;
    } else {

        // Create and allocate a new post.
        Post* new_post = malloc(sizeof(Post));

        // Check if memory allocation was successful; exits if not.
        if (new_post == NULL) {
            exit(1);
        }

        // Check if users are friends
        if (check_friends((User*) author, target) == 0) {

                strcpy(new_post->author, author->name);
                new_post->contents = contents;

                // Grab the current time (using time(NULL)) and assign to new_post. 
                time_t curr_time = time(NULL);
                time_t *date = malloc(sizeof(time_t));

                // If memory could not be allocated to date, exit and free the new_post memory.
                if (date == NULL) {
                    free(new_post);
                    exit(1);
                }
                *date = curr_time;
                new_post->date = date;

                // Change the posts in author's struct.
                Post* old_post = target->first_post;
                if (old_post == NULL) {
                    target->first_post = new_post;
                    target->next = NULL;
                } else {
                    target->first_post = new_post;
                    new_post->next = old_post;
                }

                return 0;

        } else {

            free(new_post);
            return 1;

        }

    }
}


/*
 * From the list pointed to by *user_ptr_del, delete the user
 * with the given name.
 * Remove the deleted user from any lists of friends.
 *
 * Return:
 *   - 0 on success.
 *   - 1 if a user with this name does not exist.
 */
int delete_user(const char *name, User **user_ptr_del) {
    User* user = find_user(name, *user_ptr_del);
    if (user == NULL) {
        return 1;
    }

    User *curr_user = *user_ptr_del; 
    while (curr_user != NULL) {
        // Check if the current user in the list is friends with the deleted user.
        if (check_friends(user, curr_user) == 0) {
            for (int i=0; i < MAX_FRIENDS; i++) {
                if (curr_user->friends[i] == user) {
                    curr_user->friends[i] = NULL;
                }
                user->friends[i] = NULL;
            }
        }        
    }

    // Remove all posts from the user's profile.
    Post *curr_post = user->first_post;
    Post *next_post = curr_post->next;
    if (curr_post != NULL) {
        while (next_post != NULL) {
            // Free all allocated memory from the current post.
            free(curr_post->contents);
            free(curr_post->date);
            free(curr_post->next);
            free(curr_post);

            curr_post = next_post;
            next_post = curr_post->next;
        }
    }

    // Check if user is first in the linked list. 
    if (user == *user_ptr_del) {
        // Check if the deleted user is the only user.
        if (user->next == NULL) {
            *user_ptr_del = NULL;
        } else {
            *user_ptr_del = user->next;
            user->next = NULL;
        }
    } else {

        User* prev_user = *user_ptr_del;
        User* curr_user = prev_user->next;
        User* next_user = curr_user->next;

        // Iterate until curr_user is the deleted user.
        while (curr_user != user) {
            prev_user = prev_user->next;
            curr_user = curr_user->next;
            next_user = next_user->next;
        }
        
        prev_user->next = next_user;
        curr_user->next = NULL;

    }

    // Free all alocated memory from the user.
    free(user->next);
    free(user->first_post);
    free(user);

    return 0;
}

/*
 * Make a new post from 'author' to the 'target' user,
 * containing the given contents, IF the users are friends.
 *
 * Insert the new post at the *front* of the user's list of posts.
 *
 * 'contents' is a pointer to heap-allocated memory - you do not need
 * to allocate more memory to store the contents of the post.
 *
 * Return:
 *   - 0 on success
 *   - 1 if users exist but are not friends
 *   - 2 if either User pointer is NULL
 */

My while loop is running forever.