import json


def add(client, name, score, headers, board="global"):
    client.post(
        f"/{board}/add",
        data=json.dumps({"name": name, "score": score}),
        headers=headers,
    )


class TestPlayerBest:
    def test_returns_players_highest_score(self, client, auth_headers):
        add(client, "Alice", 100, auth_headers)
        add(client, "Alice", 300, auth_headers)
        add(client, "Alice", 200, auth_headers)

        data = client.get("/global/player/Alice").get_json()
        assert data["score"] == 300

    def test_returns_200_for_known_player(self, client, auth_headers):
        add(client, "Alice", 100, auth_headers)
        assert client.get("/global/player/Alice").status_code == 200

    def test_unknown_player_returns_404(self, client):
        assert client.get("/global/player/Nobody").status_code == 404

    def test_does_not_return_other_players_entry(self, client, auth_headers):
        add(client, "Bob", 500, auth_headers)
        assert client.get("/global/player/Alice").status_code == 404

    def test_response_has_expected_fields(self, client, auth_headers):
        add(client, "Alice", 100, auth_headers)
        entry = client.get("/global/player/Alice").get_json()
        assert {"id", "name", "score", "created"} <= entry.keys()

    def test_invalid_leaderboard_returns_404(self, client):
        assert client.get("/nonexistent/player/Alice").status_code == 404

    def test_does_not_bleed_across_leaderboards(self, client, auth_headers):
        add(client, "Alice", 999, auth_headers, board="weekly")
        # Alice exists in weekly but not global
        assert client.get("/global/player/Alice").status_code == 404
