from json.decoder import JSONDecodeError
from rest_framework.views import APIView
from rest_framework.response import Response
from playlist.models import Track, Profile
from playlist.serializers import TrackSerializer
from playlist.spotify import spt
from playlist.tasks import get_banter, get_and_dither_image
from drf_spectacular.utils import extend_schema
from random import choice

class TrackListView(APIView):
    @extend_schema(
        responses={200: TrackSerializer(many=True)},
        description="Retrieve a list of all tracks."
    )
    def get(self, request):
        tracks = Track.objects.all()
        serializer = TrackSerializer(tracks, many=True, context={'request': request})
        return Response(serializer.data)

    @extend_schema(
        request=None,
        responses={201: TrackSerializer, 403: {'description': 'Forbidden'}},
        description="Create a new track from a Spotify link."
    )
    def post(self, request):
        (profile, _) = Profile.objects.get_or_create(user=request.user)
        (can_nom, reason) = profile.can_nominate
        if not can_nom:
            return Response({'error': reason}, status=403)
        (track, message) = Track.objects.create_from_spotify(request.data['spotify_link'], profile)
        serializer = TrackSerializer(track, context={'request': request})
        return Response(serializer.data)

    @extend_schema(
        request=None,
        responses={204: None, 403: {'description': 'Forbidden'}},
        description="Delete all tracks. Only superusers are allowed."
    )
    def delete(self, request):
        if request.user.is_superuser:
            Track.objects.all().delete()
            return Response(status=204)
        else:
            return Response({'error': 'Only superusers can delete all tracks'}, status=403)

class TrackDetailView(APIView):
    @extend_schema(
        responses={200: TrackSerializer},
        description="Retrieve a track by its ID."
    )
    def get(self, request, id):
        track = Track.objects.get(pk=id)
        serializer = TrackSerializer(track, context={'request': request})
        return Response(serializer.data)

    @extend_schema(
        request=None,
        responses={204: None, 403: {'description': 'Forbidden'}},
        description="Delete a single track by its ID. Only superusers are allowed."
    )
    def delete(self, request, id):
        if request.user.is_superuser:
            track = Track.objects.get(pk=id)
            track.delete()
            return Response(status=204)
        else:
            return Response({'error': 'Only superusers can delete tracks'}, status=403)

class TrackVoteView(APIView):
    @extend_schema(
        request=None,
        responses={200: TrackSerializer},
        description="Retrieve a random track for voting."
    )
    def get(self, request):
        # TODO implement a session-based vote tracking system. Random return for now.
        track_pks = Track.objects.values_list('pk', flat=True)
        if not track_pks:
            dummy = {
                "name": "Dummy",
                "artist": "Dummy",
                "album_cover": "/meowl.png"
            }
            return Response(dummy)

        random_pk = choice(track_pks)
        track = Track.objects.get(pk=random_pk)
        serializer = TrackSerializer(track, context={'request': request})
        return Response(serializer.data)
