from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
import os
import pickle
import random
import pandas as pd

def models(request):
    filename=os.path.dirname(os.path.abspath(__file__))+'/movies_pickle'
    with open(filename,'rb') as f:
        model=pickle.load(f)
    return model

def home(request):
    model=models(request)
    movie=[]
    s=model.index.size
    for i in range(21):
        r=random.randint(1,s)
        movie.append(model.index[r])
    return render(request,'home.html',{'movies':movie})

def detail(request):
    model=models(request)
    movie=request.GET['movie']
    if movie in model.index:
        return render(request,'detail.html',{'movie':movie})
    else:
        return render(request,'detail.html',{'movie':movie,'message':'Movie Not found'})


def get_similar(request,movie_name,rating):
    model=models(request)
    similar_ratings = model[movie_name]*(rating-2.5)
    similar_ratings = similar_ratings.sort_values(ascending=False)
    return similar_ratings

def recommend_movie(request,movie_collection):
    similar_movies = pd.DataFrame()
    for movie,rating in movie_collection:
        similar_movies = similar_movies.append(get_similar(request,movie,rating),ignore_index = True)
    movies=similar_movies.sum().sort_values(ascending=False).head(10).index
    return movies
    
def recommendation(request):
    movie_name=request.GET['movie']
    rating=request.GET['ratings']
    movie_collection=[]
    movie_collection.append((movie_name,int(rating)))
    movies=recommend_movie(request,movie_collection)
    return render(request,'recommendation.html',{'moviess':movies,'m':movie_name})
