# Path: foosball/games/stats.py
# def stats_red_or_blue(params):
#     """
#     Return statistics on the most important question.
#     """
#     red_wins = Game.objects.filter(teams__side=Team.RED, teams__score=10).count()
#     blue_wins = Game.objects.filter(teams__side=Team.BLUE, teams__score=10).count()
#     return {
#         "type": "pie",
#         "data": {
#             "labels": [
#                 "Red",
#                 "Blue",
#             ],
#             "datasets": [
#                 {
#                     "data": [red_wins, blue_wins],
#                     "backgroundColor": [
#                         "#FF6384",
#                         "#36A2EB",
#                     ],
#                 }]
#         },
#         "options": {}
#     }
# 
# def stats_players(params):
#     """
#     Return statistics on player singles and doubles win percentages.
#     """
#     players = Player.objects.filter(total_played__gt=5).order_by("user__username").values_list(
#         "user__username", "singles_win_percentage", "doubles_win_percentage"
#     )
#     if players:
#         usernames, singles_win_percentages, doubles_win_percentages = zip(*players)
#     else:
#         usernames = singles_win_percentages = doubles_win_percentages = []
#     return {
#         "type": "bar",
#         "data": {
#             "labels": usernames,
#             "datasets": [
#                 {
#                     "label": "Win % - singles",
#                     "data": singles_win_percentages,
#                     "backgroundColor": "#2E5168",
#                 },
#                 {
#                     "label": "Win % - doubles",
#                     "data": doubles_win_percentages,
#                     "backgroundColor": "#65923B",
#                 },
#             ]
#         },
#         "options": {}
#     }
# 
# Path: foosball/games/tests/utils.py
# def create_game(winner_loser=("RED", "BLUE"), players=(None, None)):
#     """
#     Create a game and teams.
# 
#     Optionally with given winner/loser sides and players passed in as tuples.
#     """
#     game = Game.objects.create()
#     winners = TeamFactory(score=10, game=game, side=getattr(Team, winner_loser[0]), players=players[0])
#     update_player_stats(winners)
#     losers = TeamFactory(score=5, game=game, side=getattr(Team, winner_loser[1]), players=players[1])
#     update_player_stats(losers)
#     return game, winners, losers


from decimal import Decimal
from foosball.games.stats import stats_red_or_blue, stats_players
from foosball.games.tests.utils import create_game
from foosball.users.tests.factories import UserFactory
import pytest




@pytest.mark.django_db
def test_stats_red_or_blue():
    for i in range(3):
        create_game()
    create_game(("BLUE", "RED"))
    stats = stats_red_or_blue(None)
    assert stats["data"]["datasets"][0]["data"] == [3, 1]


@pytest.mark.django_db
def test_stats_players():
    p4 = UserFactory(username="p4")
    p1 = UserFactory(username="p1")
    p3 = UserFactory(username="p3")
    p2 = UserFactory(username="p2")
    for i in range(4):
        create_game(players=([p1, p2], [p3, p4]))
    for i in range(2):

# Path: foosball/games/stats.py
# def stats_red_or_blue(params):
#     """
#     Return statistics on the most important question.
#     """
#     red_wins = Game.objects.filter(teams__side=Team.RED, teams__score=10).count()
#     blue_wins = Game.objects.filter(teams__side=Team.BLUE, teams__score=10).count()
#     return {
#         "type": "pie",
#         "data": {
#             "labels": [
#                 "Red",
#                 "Blue",
#             ],
#             "datasets": [
#                 {
#                     "data": [red_wins, blue_wins],
#                     "backgroundColor": [
#                         "#FF6384",
#                         "#36A2EB",
#                     ],
#                 }]
#         },
#         "options": {}
#     }
# 
# def stats_players(params):
#     """
#     Return statistics on player singles and doubles win percentages.
#     """
#     players = Player.objects.filter(total_played__gt=5).order_by("user__username").values_list(
#         "user__username", "singles_win_percentage", "doubles_win_percentage"
#     )
#     if players:
#         usernames, singles_win_percentages, doubles_win_percentages = zip(*players)
#     else:
#         usernames = singles_win_percentages = doubles_win_percentages = []
#     return {
#         "type": "bar",
#         "data": {
#             "labels": usernames,
#             "datasets": [
#                 {
#                     "label": "Win % - singles",
#                     "data": singles_win_percentages,
#                     "backgroundColor": "#2E5168",
#                 },
#                 {
#                     "label": "Win % - doubles",
#                     "data": doubles_win_percentages,
#                     "backgroundColor": "#65923B",
#                 },
#             ]
#         },
#         "options": {}
#     }
# 
# Path: foosball/games/tests/utils.py
# def create_game(winner_loser=("RED", "BLUE"), players=(None, None)):
#     """
#     Create a game and teams.
# 
#     Optionally with given winner/loser sides and players passed in as tuples.
#     """
#     game = Game.objects.create()
#     winners = TeamFactory(score=10, game=game, side=getattr(Team, winner_loser[0]), players=players[0])
#     update_player_stats(winners)
#     losers = TeamFactory(score=5, game=game, side=getattr(Team, winner_loser[1]), players=players[1])
#     update_player_stats(losers)
#     return game, winners, losers


# Path: foosball/games/tests/test_stats.py
from decimal import Decimal
from foosball.games.stats import stats_red_or_blue, stats_players
from foosball.games.tests.utils import create_game
from foosball.users.tests.factories import UserFactory
import pytest





@pytest.mark.django_db
def test_stats_red_or_blue():
    for i in range(3):
        create_game()
    create_game(("BLUE", "RED"))
    stats = stats_red_or_blue(None)
    assert stats["data"]["datasets"][0]["data"] == [3, 1]


@pytest.mark.django_db
def test_stats_players():
    p4 = UserFactory(username="p4")
    p1 = UserFactory(username="p1")
    p3 = UserFactory(username="p3")
    p2 = UserFactory(username="p2")
    for i in range(4):
        create_game(players=([p1, p2], [p3, p4]))
    for i in range(2):

        create_game(players=([p2], [p4]))