from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from playlist.models import Session, Profile, Track, Vote
from playlist.serializers import TrackSerializer, SessionSerializer
from drf_spectacular.utils import extend_schema

def index(request):
    return render(request, 'voting/index.html')

def session(request, session_id):
    return render(request, 'voting/session.html', {'room_name': session_id})

@extend_schema(
    responses={200: SessionSerializer(many=True)},
    description="Retrieve a list of all available sessions."
)
class SessionListView(APIView):
    def get(self, request):
        if request.user.is_superuser:
            sessions = Session.objects.all()
        else:
            sessions = Session.objects.filter(voting_open=True)
        return Response(SessionSerializer(sessions, many=True).data)

@extend_schema(
    responses={200: SessionSerializer()},
    description="Gets session details."
)
class SessionDetailView(APIView):
	def get(self, request, session_id):
		session = Session.objects.get(session_id=session_id)
		return Response(SessionSerializer(session).data)
