import random
import numpy as np
import pickle


def get_new_item_id(user_reviews, le_item, nmf_model):

    # get item embeddings
    item_embeddings = nmf_model.components_
    n_items = item_embeddings.shape[1]

    # create user item vector
    user_item_vector = np.zeros(n_items)
    rated_items = le_item.transform(user_reviews['asin'])
    ratings = np.array(user_reviews['overall'])
    for i, r in zip(rated_items,ratings):
        user_item_vector[i] = r

    # make predictions
    user_embeddings = nmf_model.transform(user_item_vector.reshape(1, -1))
    item_embeddings = nmf_model.components_
    predictions = np.squeeze(np.dot(user_embeddings, item_embeddings))
    sorted_items = np.argsort(-predictions)  # want to sort in reverse order

    # filter items and inverse encoding
    filtered_items = [le_item.inverse_transform(item) for item in sorted_items
                      if le_item.inverse_transform(item) not in list(user_reviews['asin'])]

    # return top item
    return filtered_items[0]