from rest_framework import serializers
from .models import GameModel, GameMembersModel

class GameSerializer(serializers.ModelSerializer):

    players_id = serializers.SerializerMethodField()
    winner_id = serializers.ReadOnlyField()
    state = serializers.SerializerMethodField()
    started = serializers.ReadOnlyField()
    finished = serializers.ReadOnlyField()

    class Meta:
        model = GameModel
        fields = ["id", "winner_id", "state", "started", "finished", "players_id"]

    def get_state(self, instance: GameModel):
        if (instance.finished):
            return "finished"
        if (instance.started):
            return "started"
        return "waiting"

    def get_players_id(self, instance: GameModel):
        players_id = [player_game.player_id for player_game in GameMembersModel.objects.filter(game_id = instance.pk)]
        return players_id