# Path: src/moviesapp/views/mixins.py
# class AjaxAnonymousView(JsonRequestResponseMixin, View):
#     def success(self, **kwargs):
#         response = {"status": "success"}
#         response.update(kwargs)
#         return self.render_json_response(response)
# 
# class AjaxView(LoginRequiredMixin, AjaxAnonymousView):
#     raise_exception = True
# 
# class TemplateAnonymousView(TemplateViewOriginal):
#     pass
# 
# Path: src/moviesapp/views/utils.py
# def add_movie_to_list(movie_id, list_id, user):
#     record = user.get_records().filter(movie_id=movie_id)
#     if record.exists():
#         record = record[0]
#         if record.list_id != list_id:
#             ActionRecord(action_id=2, user=user, movie_id=movie_id, list_id=list_id).save()
#             record.list_id = list_id
#             record.date = datetime.today()
#             record.save()
#     else:
#         record = Record(movie_id=movie_id, list_id=list_id, user=user)
#         record.save()
#         ActionRecord(action_id=1, user=user, movie_id=movie_id, list_id=list_id).save()


import json
from sentry_sdk import capture_exception
from moviesapp.exceptions import MovieNotInDb, NotAvailableSearchType
from moviesapp.models import Movie
from moviesapp.tmdb import get_movies_from_tmdb
from moviesapp.utils import add_movie_to_db
from .mixins import AjaxAnonymousView, AjaxView, TemplateAnonymousView
from .utils import add_movie_to_list




class SearchView(TemplateAnonymousView):
    template_name = "search.html"


class SearchMovieView(AjaxAnonymousView):
    def get(self, request):
        AVAILABLE_SEARCH_TYPES = [
            "actor",
            "movie",
            "director",
        ]
        try:
            GET = request.GET
            query = GET["query"]
            options = json.loads(GET["options"])
            type_ = GET["type"]
            if type_ not in AVAILABLE_SEARCH_TYPES:
                raise NotAvailableSearchType
        except (KeyError, NotAvailableSearchType):
            return self.render_bad_request_response()
        movies = get_movies_from_tmdb(query, type_, options, request.user, self.request.LANGUAGE_CODE)
        return self.success(movies=movies)


class AddToListFromDbView(AjaxView):
    @staticmethod

# Path: src/moviesapp/views/mixins.py
# class AjaxAnonymousView(JsonRequestResponseMixin, View):
#     def success(self, **kwargs):
#         response = {"status": "success"}
#         response.update(kwargs)
#         return self.render_json_response(response)
# 
# class AjaxView(LoginRequiredMixin, AjaxAnonymousView):
#     raise_exception = True
# 
# class TemplateAnonymousView(TemplateViewOriginal):
#     pass
# 
# Path: src/moviesapp/views/utils.py
# def add_movie_to_list(movie_id, list_id, user):
#     record = user.get_records().filter(movie_id=movie_id)
#     if record.exists():
#         record = record[0]
#         if record.list_id != list_id:
#             ActionRecord(action_id=2, user=user, movie_id=movie_id, list_id=list_id).save()
#             record.list_id = list_id
#             record.date = datetime.today()
#             record.save()
#     else:
#         record = Record(movie_id=movie_id, list_id=list_id, user=user)
#         record.save()
#         ActionRecord(action_id=1, user=user, movie_id=movie_id, list_id=list_id).save()


# Path: src/moviesapp/views/search.py
import json
from sentry_sdk import capture_exception
from moviesapp.exceptions import MovieNotInDb, NotAvailableSearchType
from moviesapp.models import Movie
from moviesapp.tmdb import get_movies_from_tmdb
from moviesapp.utils import add_movie_to_db
from .mixins import AjaxAnonymousView, AjaxView, TemplateAnonymousView
from .utils import add_movie_to_list





class SearchView(TemplateAnonymousView):
    template_name = "search.html"


class SearchMovieView(AjaxAnonymousView):
    def get(self, request):
        AVAILABLE_SEARCH_TYPES = [
            "actor",
            "movie",
            "director",
        ]
        try:
            GET = request.GET
            query = GET["query"]
            options = json.loads(GET["options"])
            type_ = GET["type"]
            if type_ not in AVAILABLE_SEARCH_TYPES:
                raise NotAvailableSearchType
        except (KeyError, NotAvailableSearchType):
            return self.render_bad_request_response()
        movies = get_movies_from_tmdb(query, type_, options, request.user, self.request.LANGUAGE_CODE)
        return self.success(movies=movies)


class AddToListFromDbView(AjaxView):
    @staticmethod

    def _get_movie_id(tmdb_id):