from django.shortcuts import render
from django.http import HttpResponse
from django.http import JsonResponse
from django.db.models import F
from django.views.decorators.csrf import csrf_exempt

from .models import Movie, Person


def home(request):
    context = {"sections": Movie.objects.home_pick()}
    return render(request, "home.html", context)


def search(request):
    if request.GET.get("q"):
        query = request.GET["q"]
    else:
        query = ""
    context = {"movies": Movie.objects.get_movies(query), "query": query}
    return render(request, "search.html", context)


def about(request):
    context = {}
    return render(request, "about.html", context)


def help(request):
    context = {}
    return render(request, "help.html", context)


def bugs(request):
    context = {}
    return render(request, "bugs.html", context)


def movie(request, id):
    context = {
        "movie": Movie.objects.get_movie_by_id(id),
        "subs": Movie.objects.get_subs(id),
    }
    return render(request, "movie.html", context)


def api(request):
    context = Movie.objects.api(request)
    return JsonResponse(context)


def error_404(request, exception):
    return render(request, "404.html")
