from db_admin.sql_helper import getConn
import exploit
import explore
import numpy as np
import pandas as pd


def get_next_item(user_id, le_item, nmf_model):
    """Returns an item_id based on the user_id."""

    # Get all reviews from this user
    query = """SELECT * FROM reviews WHERE reviewerid = '{}'""".format(user_id)
    conn = getConn('db_admin/creds.json')
    user_reviews = pd.read_sql(query, conn)

    # Decide whether to explore or exploit
    if (len(user_reviews) < 5) | (np.random.rand() < 0.2):
        print 'exploring'
        return explore.get_new_item_id(user_reviews)
    else:
        print 'exploiting'
        return exploit.get_new_item_id(user_reviews, le_item, nmf_model)