from django.shortcuts import redirect, render
from django.urls import reverse
from ...models import Lists, Activities

from django.views import View

from songs.models import Song


class AddToListView(View):
    """Endpoit to add a song to the list of a user"""

    def post(self, request, songId):
        if request.user.is_authenticated:
            try:
                song = Song.objects.get(id=songId)
            except Song.DoesNotExist:
                return render(request, "404.html")

            try:
                list_entry = Lists.objects.get(user=request.user, song=song)
                list_entry.delete()
                action = "REMOVED"
            except:
                list_entry = Lists(user=request.user, song=song, favourite=False)
                list_entry.save()
                action = "ADDED"

            activity = Activities(song=song, user=request.user, action=action)
            activity.save()

        return redirect(reverse("song", args=[songId]))
