from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import permissions, status

from django.http import HttpRequest

from .models import GameMembersModel, GameModel
from .serializers import GameSerializer

from . import config

class GameHistoryView(APIView):
    
    permission_classes = (permissions.AllowAny,)

    def get(self, request: HttpRequest, pk: int = None):
        
        member_game_model_list: list[GameMembersModel] = GameMembersModel.objects.filter(player_id = pk)
        
        game_model_list: list[GameModel] = []
        
        for member_game_model in member_game_model_list:
            game_model_list.append(GameModel.objects.get(pk = member_game_model.game_id))
        
        games_data: list[dict] = [GameSerializer(game_model).data for game_model in game_model_list]
        
        return Response(games_data)