"""Tests for cumestatsplayer models."""

import pytest

from fastbreak.models.cume_stats_player import (
    CumeStatsPlayerResponse,
    GameByGameStat,
    TotalPlayerStat,
)


class TestGameByGameStat:
    """Tests for GameByGameStat model."""

    def test_parse_valid_entry(self):
        """GameByGameStat parses a valid game entry."""
        data = {
            "DATE_EST": "01/20/2026",
            "VISITOR_TEAM": "Lakers",
            "HOME_TEAM": "Nuggets",
            "GP": 1,
            "GS": 1,
            "ACTUAL_MINUTES": 34,
            "ACTUAL_SECONDS": 25,
            "FG": 8,
            "FGA": 15,
            "FG_PCT": 0.533,
            "FG3": 1,
            "FG3A": 1,
            "FG3_PCT": 1.0,
            "FT": 2,
            "FTA": 6,
            "FT_PCT": 0.333,
            "OFF_REB": 1,
            "DEF_REB": 8,
            "TOT_REB": 9,
            "AVG_TOT_REB": 9.0,
            "AST": 8,
            "PF": 2,
            "DQ": 0,
            "STL": 0,
            "TURNOVERS": 6,
            "BLK": 0,
            "PTS": 19,
            "AVG_PTS": 19.0,
        }

        stat = GameByGameStat.model_validate(data)

        assert stat.date_est == "01/20/2026"
        assert stat.visitor_team == "Lakers"
        assert stat.home_team == "Nuggets"
        assert stat.gp == 1
        assert stat.gs == 1
        assert stat.actual_minutes == 34
        assert stat.pts == 19
        assert stat.fg_pct == pytest.approx(0.533)
        assert stat.turnovers == 6

    def test_parse_zero_stats(self):
        """GameByGameStat handles zero values correctly."""
        data = {
            "DATE_EST": "01/01/2026",
            "VISITOR_TEAM": "Team A",
            "HOME_TEAM": "Team B",
            "GP": 1,
            "GS": 0,
            "ACTUAL_MINUTES": 0,
            "ACTUAL_SECONDS": 0,
            "FG": 0,
            "FGA": 0,
            "FG_PCT": 0.0,
            "FG3": 0,
            "FG3A": 0,
            "FG3_PCT": 0.0,
            "FT": 0,
            "FTA": 0,
            "FT_PCT": 0.0,
            "OFF_REB": 0,
            "DEF_REB": 0,
            "TOT_REB": 0,
            "AVG_TOT_REB": 0.0,
            "AST": 0,
            "PF": 0,
            "DQ": 0,
            "STL": 0,
            "TURNOVERS": 0,
            "BLK": 0,
            "PTS": 0,
            "AVG_PTS": 0.0,
        }

        stat = GameByGameStat.model_validate(data)

        assert stat.pts == 0
        assert stat.fg_pct == 0.0
        assert stat.gs == 0


class TestTotalPlayerStat:
    """Tests for TotalPlayerStat model."""

    def test_parse_valid_entry(self):
        """TotalPlayerStat parses a valid entry with all fields."""
        data = {
            "DISPLAY_FI_LAST": "L. James",
            "PERSON_ID": 2544,
            "JERSEY_NUM": "23",
            "GP": 1,
            "GS": 1,
            "ACTUAL_MINUTES": 34,
            "ACTUAL_SECONDS": 25,
            "FG": 8,
            "FGA": 15,
            "FG_PCT": 0.533,
            "FG3": 1,
            "FG3A": 1,
            "FG3_PCT": 1.0,
            "FT": 2,
            "FTA": 6,
            "FT_PCT": 0.333,
            "OFF_REB": 1,
            "DEF_REB": 8,
            "TOT_REB": 9,
            "AST": 8,
            "PF": 2,
            "DQ": 0,
            "STL": 0,
            "TURNOVERS": 6,
            "BLK": 0,
            "PTS": 19,
            "MAX_ACTUAL_MINUTES": 34,
            "MAX_ACTUAL_SECONDS": 25,
            "MAX_REB": 9,
            "MAX_AST": 8,
            "MAX_STL": 0,
            "MAX_TURNOVERS": 6,
            "MAX_BLK": 0,
            "MAX_PTS": 19,
            "AVG_ACTUAL_MINUTES": 34,
            "AVG_ACTUAL_SECONDS": 25.0,
            "AVG_TOT_REB": 9.0,
            "AVG_AST": 8.0,
            "AVG_STL": 0.0,
            "AVG_TURNOVERS": 6.0,
            "AVG_BLK": 0.0,
            "AVG_PTS": 19.0,
            "PER_MIN_TOT_REB": 12.6,
            "PER_MIN_AST": 11.2,
            "PER_MIN_STL": 0.0,
            "PER_MIN_TURNOVERS": 8.4,
            "PER_MIN_BLK": 0.0,
            "PER_MIN_PTS": 26.5,
        }

        stat = TotalPlayerStat.model_validate(data)

        assert stat.display_fi_last == "L. James"
        assert stat.person_id == 2544
        assert stat.jersey_num == "23"
        assert stat.pts == 19
        assert stat.max_pts == 19
        assert stat.avg_pts == pytest.approx(19.0)
        assert stat.per_min_pts == pytest.approx(26.5)

    def test_parse_with_zero_per_minute_stats(self):
        """TotalPlayerStat handles zero per-minute stats."""
        data = {
            "DISPLAY_FI_LAST": "T. Player",
            "PERSON_ID": 1,
            "JERSEY_NUM": "0",
            "GP": 1,
            "GS": 0,
            "ACTUAL_MINUTES": 0,
            "ACTUAL_SECONDS": 0,
            "FG": 0,
            "FGA": 0,
            "FG_PCT": 0.0,
            "FG3": 0,
            "FG3A": 0,
            "FG3_PCT": 0.0,
            "FT": 0,
            "FTA": 0,
            "FT_PCT": 0.0,
            "OFF_REB": 0,
            "DEF_REB": 0,
            "TOT_REB": 0,
            "AST": 0,
            "PF": 0,
            "DQ": 0,
            "STL": 0,
            "TURNOVERS": 0,
            "BLK": 0,
            "PTS": 0,
            "MAX_ACTUAL_MINUTES": 0,
            "MAX_ACTUAL_SECONDS": 0,
            "MAX_REB": 0,
            "MAX_AST": 0,
            "MAX_STL": 0,
            "MAX_TURNOVERS": 0,
            "MAX_BLK": 0,
            "MAX_PTS": 0,
            "AVG_ACTUAL_MINUTES": 0,
            "AVG_ACTUAL_SECONDS": 0.0,
            "AVG_TOT_REB": 0.0,
            "AVG_AST": 0.0,
            "AVG_STL": 0.0,
            "AVG_TURNOVERS": 0.0,
            "AVG_BLK": 0.0,
            "AVG_PTS": 0.0,
            "PER_MIN_TOT_REB": 0.0,
            "PER_MIN_AST": 0.0,
            "PER_MIN_STL": 0.0,
            "PER_MIN_TURNOVERS": 0.0,
            "PER_MIN_BLK": 0.0,
            "PER_MIN_PTS": 0.0,
        }

        stat = TotalPlayerStat.model_validate(data)

        assert stat.per_min_pts == 0.0
        assert stat.avg_pts == 0.0


class TestCumeStatsPlayerResponse:
    """Tests for CumeStatsPlayerResponse model."""

    def test_parse_full_response(self):
        """CumeStatsPlayerResponse parses both result sets."""
        data = {
            "resource": "cumestatsplayer",
            "parameters": {
                "LeagueID": "00",
                "Season": "2025-26",
                "SeasonType": "Regular Season",
                "PlayerID": 2544,
                "GameIDs": "0022500617",
            },
            "resultSets": [
                {
                    "name": "GameByGameStats",
                    "headers": [
                        "DATE_EST",
                        "VISITOR_TEAM",
                        "HOME_TEAM",
                        "GP",
                        "GS",
                        "ACTUAL_MINUTES",
                        "ACTUAL_SECONDS",
                        "FG",
                        "FGA",
                        "FG_PCT",
                        "FG3",
                        "FG3A",
                        "FG3_PCT",
                        "FT",
                        "FTA",
                        "FT_PCT",
                        "OFF_REB",
                        "DEF_REB",
                        "TOT_REB",
                        "AVG_TOT_REB",
                        "AST",
                        "PF",
                        "DQ",
                        "STL",
                        "TURNOVERS",
                        "BLK",
                        "PTS",
                        "AVG_PTS",
                    ],
                    "rowSet": [
                        [
                            "01/20/2026",
                            "Lakers",
                            "Nuggets",
                            1,
                            1,
                            34,
                            25,
                            8,
                            15,
                            0.533,
                            1,
                            1,
                            1.0,
                            2,
                            6,
                            0.333,
                            1,
                            8,
                            9,
                            9.0,
                            8,
                            2,
                            0,
                            0,
                            6,
                            0,
                            19,
                            19.0,
                        ],
                    ],
                },
                {
                    "name": "TotalPlayerStats",
                    "headers": [
                        "DISPLAY_FI_LAST",
                        "PERSON_ID",
                        "JERSEY_NUM",
                        "GP",
                        "GS",
                        "ACTUAL_MINUTES",
                        "ACTUAL_SECONDS",
                        "FG",
                        "FGA",
                        "FG_PCT",
                        "FG3",
                        "FG3A",
                        "FG3_PCT",
                        "FT",
                        "FTA",
                        "FT_PCT",
                        "OFF_REB",
                        "DEF_REB",
                        "TOT_REB",
                        "AST",
                        "PF",
                        "DQ",
                        "STL",
                        "TURNOVERS",
                        "BLK",
                        "PTS",
                        "MAX_ACTUAL_MINUTES",
                        "MAX_ACTUAL_SECONDS",
                        "MAX_REB",
                        "MAX_AST",
                        "MAX_STL",
                        "MAX_TURNOVERS",
                        "MAX_BLK",
                        "MAX_PTS",
                        "AVG_ACTUAL_MINUTES",
                        "AVG_ACTUAL_SECONDS",
                        "AVG_TOT_REB",
                        "AVG_AST",
                        "AVG_STL",
                        "AVG_TURNOVERS",
                        "AVG_BLK",
                        "AVG_PTS",
                        "PER_MIN_TOT_REB",
                        "PER_MIN_AST",
                        "PER_MIN_STL",
                        "PER_MIN_TURNOVERS",
                        "PER_MIN_BLK",
                        "PER_MIN_PTS",
                    ],
                    "rowSet": [
                        [
                            "L. James",
                            2544,
                            "23",
                            1,
                            1,
                            34,
                            25,
                            8,
                            15,
                            0.533,
                            1,
                            1,
                            1.0,
                            2,
                            6,
                            0.333,
                            1,
                            8,
                            9,
                            8,
                            2,
                            0,
                            0,
                            6,
                            0,
                            19,
                            34,
                            25,
                            9,
                            8,
                            0,
                            6,
                            0,
                            19,
                            34,
                            25.0,
                            9.0,
                            8.0,
                            0.0,
                            6.0,
                            0.0,
                            19.0,
                            12.6,
                            11.2,
                            0.0,
                            8.4,
                            0.0,
                            26.5,
                        ],
                    ],
                },
            ],
        }

        response = CumeStatsPlayerResponse.model_validate(data)

        # Check game stats
        assert len(response.game_by_game_stats) == 1
        assert response.game_by_game_stats[0].home_team == "Nuggets"
        assert response.game_by_game_stats[0].pts == 19

        # Check total stats
        assert response.total_player_stats is not None
        assert response.total_player_stats.display_fi_last == "L. James"
        assert response.total_player_stats.person_id == 2544

    def test_handles_multiple_games(self):
        """CumeStatsPlayerResponse handles multiple games in GameByGameStats."""
        data = {
            "resultSets": [
                {
                    "name": "GameByGameStats",
                    "headers": [
                        "DATE_EST",
                        "VISITOR_TEAM",
                        "HOME_TEAM",
                        "GP",
                        "GS",
                        "ACTUAL_MINUTES",
                        "ACTUAL_SECONDS",
                        "FG",
                        "FGA",
                        "FG_PCT",
                        "FG3",
                        "FG3A",
                        "FG3_PCT",
                        "FT",
                        "FTA",
                        "FT_PCT",
                        "OFF_REB",
                        "DEF_REB",
                        "TOT_REB",
                        "AVG_TOT_REB",
                        "AST",
                        "PF",
                        "DQ",
                        "STL",
                        "TURNOVERS",
                        "BLK",
                        "PTS",
                        "AVG_PTS",
                    ],
                    "rowSet": [
                        [
                            "01/18/2026",
                            "Lakers",
                            "Celtics",
                            1,
                            1,
                            36,
                            0,
                            10,
                            20,
                            0.5,
                            2,
                            5,
                            0.4,
                            3,
                            4,
                            0.75,
                            2,
                            6,
                            8,
                            8.0,
                            10,
                            1,
                            0,
                            2,
                            3,
                            1,
                            25,
                            25.0,
                        ],
                        [
                            "01/20/2026",
                            "Lakers",
                            "Nuggets",
                            2,
                            2,
                            34,
                            25,
                            8,
                            15,
                            0.533,
                            1,
                            1,
                            1.0,
                            2,
                            6,
                            0.333,
                            1,
                            8,
                            9,
                            8.5,
                            8,
                            2,
                            0,
                            0,
                            6,
                            0,
                            19,
                            22.0,
                        ],
                    ],
                },
                {
                    "name": "TotalPlayerStats",
                    "headers": [
                        "DISPLAY_FI_LAST",
                        "PERSON_ID",
                        "JERSEY_NUM",
                        "GP",
                        "GS",
                        "ACTUAL_MINUTES",
                        "ACTUAL_SECONDS",
                        "FG",
                        "FGA",
                        "FG_PCT",
                        "FG3",
                        "FG3A",
                        "FG3_PCT",
                        "FT",
                        "FTA",
                        "FT_PCT",
                        "OFF_REB",
                        "DEF_REB",
                        "TOT_REB",
                        "AST",
                        "PF",
                        "DQ",
                        "STL",
                        "TURNOVERS",
                        "BLK",
                        "PTS",
                        "MAX_ACTUAL_MINUTES",
                        "MAX_ACTUAL_SECONDS",
                        "MAX_REB",
                        "MAX_AST",
                        "MAX_STL",
                        "MAX_TURNOVERS",
                        "MAX_BLK",
                        "MAX_PTS",
                        "AVG_ACTUAL_MINUTES",
                        "AVG_ACTUAL_SECONDS",
                        "AVG_TOT_REB",
                        "AVG_AST",
                        "AVG_STL",
                        "AVG_TURNOVERS",
                        "AVG_BLK",
                        "AVG_PTS",
                        "PER_MIN_TOT_REB",
                        "PER_MIN_AST",
                        "PER_MIN_STL",
                        "PER_MIN_TURNOVERS",
                        "PER_MIN_BLK",
                        "PER_MIN_PTS",
                    ],
                    "rowSet": [
                        [
                            "L. James",
                            2544,
                            "23",
                            2,
                            2,
                            70,
                            25,
                            18,
                            35,
                            0.514,
                            3,
                            6,
                            0.5,
                            5,
                            10,
                            0.5,
                            3,
                            14,
                            17,
                            18,
                            3,
                            0,
                            2,
                            9,
                            1,
                            44,
                            36,
                            0,
                            9,
                            10,
                            2,
                            6,
                            1,
                            25,
                            35,
                            12.5,
                            8.5,
                            9.0,
                            1.0,
                            4.5,
                            0.5,
                            22.0,
                            12.1,
                            10.2,
                            0.6,
                            5.1,
                            0.3,
                            24.8,
                        ],
                    ],
                },
            ],
        }

        response = CumeStatsPlayerResponse.model_validate(data)

        assert len(response.game_by_game_stats) == 2
        assert response.game_by_game_stats[0].home_team == "Celtics"
        assert response.game_by_game_stats[1].home_team == "Nuggets"
        assert response.total_player_stats is not None
        assert response.total_player_stats.gp == 2

    def test_handles_empty_game_stats(self):
        """CumeStatsPlayerResponse handles empty GameByGameStats."""
        data = {
            "resultSets": [
                {
                    "name": "GameByGameStats",
                    "headers": [
                        "DATE_EST",
                        "VISITOR_TEAM",
                        "HOME_TEAM",
                        "GP",
                        "GS",
                        "ACTUAL_MINUTES",
                        "ACTUAL_SECONDS",
                        "FG",
                        "FGA",
                        "FG_PCT",
                        "FG3",
                        "FG3A",
                        "FG3_PCT",
                        "FT",
                        "FTA",
                        "FT_PCT",
                        "OFF_REB",
                        "DEF_REB",
                        "TOT_REB",
                        "AVG_TOT_REB",
                        "AST",
                        "PF",
                        "DQ",
                        "STL",
                        "TURNOVERS",
                        "BLK",
                        "PTS",
                        "AVG_PTS",
                    ],
                    "rowSet": [],
                },
                {
                    "name": "TotalPlayerStats",
                    "headers": [
                        "DISPLAY_FI_LAST",
                        "PERSON_ID",
                        "JERSEY_NUM",
                        "GP",
                        "GS",
                        "ACTUAL_MINUTES",
                        "ACTUAL_SECONDS",
                        "FG",
                        "FGA",
                        "FG_PCT",
                        "FG3",
                        "FG3A",
                        "FG3_PCT",
                        "FT",
                        "FTA",
                        "FT_PCT",
                        "OFF_REB",
                        "DEF_REB",
                        "TOT_REB",
                        "AST",
                        "PF",
                        "DQ",
                        "STL",
                        "TURNOVERS",
                        "BLK",
                        "PTS",
                        "MAX_ACTUAL_MINUTES",
                        "MAX_ACTUAL_SECONDS",
                        "MAX_REB",
                        "MAX_AST",
                        "MAX_STL",
                        "MAX_TURNOVERS",
                        "MAX_BLK",
                        "MAX_PTS",
                        "AVG_ACTUAL_MINUTES",
                        "AVG_ACTUAL_SECONDS",
                        "AVG_TOT_REB",
                        "AVG_AST",
                        "AVG_STL",
                        "AVG_TURNOVERS",
                        "AVG_BLK",
                        "AVG_PTS",
                        "PER_MIN_TOT_REB",
                        "PER_MIN_AST",
                        "PER_MIN_STL",
                        "PER_MIN_TURNOVERS",
                        "PER_MIN_BLK",
                        "PER_MIN_PTS",
                    ],
                    "rowSet": [],
                },
            ],
        }

        response = CumeStatsPlayerResponse.model_validate(data)

        assert len(response.game_by_game_stats) == 0
        assert response.total_player_stats is None

    def test_parse_direct_dict(self):
        """CumeStatsPlayerResponse handles already-parsed dict format."""
        data = {
            "game_by_game_stats": [],
            "total_player_stats": None,
        }

        response = CumeStatsPlayerResponse.model_validate(data)

        assert len(response.game_by_game_stats) == 0
        assert response.total_player_stats is None
