from flask import Flask, request, session, render_template, redirect, url_for, g
from flask_restful import Api, Resource, reqparse
import os, ast
from pymongo import MongoClient
from dotenv import load_dotenv
from database.connection import createConnectionMongoDB
from engine.request_queries import trend_recommendation, homepage_recommendation, collaborative_filter

app = Flask(__name__)
api = Api(app)

# We define these variables to (optionally) connect to an external MongoDB
# instance.
envvals = ["MONGODBUSER", "MONGODBPASSWORD", "MONGODBSERVER"]
dbstring = 'mongodb+srv://{0}:{1}@{2}/test?retryWrites=true&w=majority'

# Since we are asked to pass a class rather than an instance of the class to the
# add_resource method, we open the connection to the database outside of the 
# Recom class.
load_dotenv()
if os.getenv(envvals[0]) is not None:
    envvals = list(map(lambda x: str(os.getenv(x)), envvals))
    client = createConnectionMongoDB()
else:
    client = createConnectionMongoDB()
database = client.huwebshop


class Recom(Resource):
    """ This class represents the REST API that provides the recommendations for
    the webshop. At the moment, the API simply returns a random set of products
    to recommend."""

    def get(self, profileid, count, type, productid):
        """ This function represents the handler for GET requests coming in
        through the API. It currently returns a random sample of products. """
        # randcursor = database.products.aggregate([{'$sample': {'size': count}}])
        # prodids = list(map(lambda x: x['_id'], list(randcursor)))
        prodids = []
        if type == "homepage":
            prodids = homepage_recommendation(profileid)
        elif type == 'shoppingcart':
            prodids = trend_recommendation(count, profileid)
        elif type == "products":
            prodids = collaborative_filter(count, profileid)
        return prodids, 200


# This method binds the Recom class to the REST API, to parse specifically
# requests in the format described below.
api.add_resource(Recom, "/<string:profileid>/<int:count>/<string:type>/<string:productid>")
