"""Tests for cumestatsteam models."""

import pytest

from fastbreak.models.cume_stats_team import (
    CumeStatsTeamResponse,
    TeamPlayerStat,
    TotalTeamStat,
)


class TestTeamPlayerStat:
    """Tests for TeamPlayerStat model."""

    def test_parse_valid_entry(self):
        """TeamPlayerStat parses a valid player entry."""
        data = {
            "JERSEY_NUM": "23",
            "PLAYER": "LeBron James",
            "PERSON_ID": 2544,
            "TEAM_ID": 1610612747,
            "GP": 2,
            "GS": 2,
            "ACTUAL_MINUTES": 70,
            "ACTUAL_SECONDS": 30,
            "FG": 18,
            "FGA": 35,
            "FG_PCT": 0.514,
            "FG3": 3,
            "FG3A": 8,
            "FG3_PCT": 0.375,
            "FT": 5,
            "FTA": 8,
            "FT_PCT": 0.625,
            "OFF_REB": 2,
            "DEF_REB": 14,
            "TOT_REB": 16,
            "AST": 15,
            "PF": 4,
            "DQ": 0,
            "STL": 2,
            "TURNOVERS": 8,
            "BLK": 1,
            "PTS": 44,
            "MAX_ACTUAL_MINUTES": 36,
            "MAX_ACTUAL_SECONDS": 15,
            "MAX_REB": 9,
            "MAX_AST": 10,
            "MAX_STL": 2,
            "MAX_TURNOVERS": 6,
            "MAX_BLKP": 1,
            "MAX_PTS": 25,
            "AVG_ACTUAL_MINUTES": 35.0,
            "AVG_ACTUAL_SECONDS": 15.0,
            "AVG_REB": 8.0,
            "AVG_AST": 7.5,
            "AVG_STL": 1.0,
            "AVG_TURNOVERS": 4.0,
            "AVG_BLKP": 0.5,
            "AVG_PTS": 22.0,
            "PER_MIN_REB": 10.9,
            "PER_MIN_AST": 10.3,
            "PER_MIN_STL": 1.4,
            "PER_MIN_TURNOVERS": 5.5,
            "PER_MIN_BLK": 0.7,
            "PER_MIN_PTS": 30.1,
        }

        stat = TeamPlayerStat.model_validate(data)

        assert stat.jersey_num == "23"
        assert stat.player == "LeBron James"
        assert stat.person_id == 2544
        assert stat.pts == 44
        assert stat.fg_pct == pytest.approx(0.514)
        assert stat.avg_pts == pytest.approx(22.0)


class TestTotalTeamStat:
    """Tests for TotalTeamStat model."""

    def test_parse_valid_entry(self):
        """TotalTeamStat parses a valid team totals entry."""
        data = {
            "CITY": "Los Angeles",
            "NICKNAME": "Lakers",
            "TEAM_ID": 1610612747,
            "W": 1,
            "L": 1,
            "W_HOME": 1,
            "L_HOME": 0,
            "W_ROAD": 0,
            "L_ROAD": 1,
            "TEAM_TURNOVERS": 2,
            "TEAM_REBOUNDS": 20,
            "GP": 2,
            "GS": 10,
            "ACTUAL_MINUTES": 480,
            "ACTUAL_SECONDS": 0,
            "FG": 85,
            "FGA": 175,
            "FG_PCT": 0.486,
            "FG3": 28,
            "FG3A": 70,
            "FG3_PCT": 0.400,
            "FT": 30,
            "FTA": 40,
            "FT_PCT": 0.750,
            "OFF_REB": 12,
            "DEF_REB": 70,
            "TOT_REB": 102,
            "AST": 55,
            "PF": 40,
            "STL": 12,
            "TOTAL_TURNOVERS": 22,
            "BLK": 8,
            "PTS": 228,
            "AVG_REB": 51.0,
            "AVG_PTS": 114.0,
            "DQ": 0,
        }

        stat = TotalTeamStat.model_validate(data)

        assert stat.city == "Los Angeles"
        assert stat.nickname == "Lakers"
        assert stat.team_id == 1610612747
        assert stat.w == 1
        assert stat.losses == 1
        assert stat.pts == 228
        assert stat.avg_pts == pytest.approx(114.0)

    def test_parse_opponents_entry(self):
        """TotalTeamStat parses opponent stats (CITY='OPPONENTS')."""
        data = {
            "CITY": "OPPONENTS",
            "NICKNAME": " ",
            "TEAM_ID": 0,
            "W": 1,
            "L": 1,
            "W_HOME": 0,
            "L_HOME": 1,
            "W_ROAD": 1,
            "L_ROAD": 0,
            "TEAM_TURNOVERS": 1,
            "TEAM_REBOUNDS": 23,
            "GP": 2,
            "GS": 10,
            "ACTUAL_MINUTES": 480,
            "ACTUAL_SECONDS": 0,
            "FG": 81,
            "FGA": 170,
            "FG_PCT": 0.476,
            "FG3": 25,
            "FG3A": 62,
            "FG3_PCT": 0.403,
            "FT": 35,
            "FTA": 51,
            "FT_PCT": 0.686,
            "OFF_REB": 15,
            "DEF_REB": 67,
            "TOT_REB": 105,
            "AST": 51,
            "PF": 39,
            "STL": 9,
            "TOTAL_TURNOVERS": 18,
            "BLK": 9,
            "PTS": 222,
            "AVG_REB": 52.5,
            "AVG_PTS": 111.0,
            "DQ": 0,
        }

        stat = TotalTeamStat.model_validate(data)

        assert stat.city == "OPPONENTS"
        assert stat.team_id == 0
        assert stat.pts == 222


class TestCumeStatsTeamResponse:
    """Tests for CumeStatsTeamResponse model."""

    def test_parse_full_response(self):
        """CumeStatsTeamResponse parses both result sets."""
        data = {
            "resource": "cumestatsteam",
            "parameters": {"TeamID": 1610612745},
            "resultSets": [
                {
                    "name": "GameByGameStats",
                    "headers": [
                        "JERSEY_NUM",
                        "PLAYER",
                        "PERSON_ID",
                        "TEAM_ID",
                        "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_BLKP",
                        "MAX_PTS",
                        "AVG_ACTUAL_MINUTES",
                        "AVG_ACTUAL_SECONDS",
                        "AVG_REB",
                        "AVG_AST",
                        "AVG_STL",
                        "AVG_TURNOVERS",
                        "AVG_BLKP",
                        "AVG_PTS",
                        "PER_MIN_REB",
                        "PER_MIN_AST",
                        "PER_MIN_STL",
                        "PER_MIN_TURNOVERS",
                        "PER_MIN_BLK",
                        "PER_MIN_PTS",
                    ],
                    "rowSet": [
                        [
                            "23",
                            "Player A",
                            1,
                            100,
                            1,
                            1,
                            30,
                            0,
                            8,
                            15,
                            0.533,
                            2,
                            5,
                            0.4,
                            3,
                            4,
                            0.75,
                            1,
                            5,
                            6,
                            5,
                            2,
                            0,
                            1,
                            3,
                            1,
                            21,
                            30,
                            0,
                            6,
                            5,
                            1,
                            3,
                            1,
                            21,
                            30.0,
                            0.0,
                            6.0,
                            5.0,
                            1.0,
                            3.0,
                            1.0,
                            21.0,
                            12.0,
                            10.0,
                            2.0,
                            6.0,
                            2.0,
                            42.0,
                        ],
                        [
                            "10",
                            "Player B",
                            2,
                            100,
                            1,
                            1,
                            25,
                            0,
                            6,
                            12,
                            0.5,
                            1,
                            3,
                            0.333,
                            2,
                            2,
                            1.0,
                            2,
                            4,
                            6,
                            3,
                            3,
                            0,
                            2,
                            2,
                            0,
                            15,
                            25,
                            0,
                            6,
                            3,
                            2,
                            2,
                            0,
                            15,
                            25.0,
                            0.0,
                            6.0,
                            3.0,
                            2.0,
                            2.0,
                            0.0,
                            15.0,
                            14.4,
                            7.2,
                            4.8,
                            4.8,
                            0.0,
                            36.0,
                        ],
                    ],
                },
                {
                    "name": "TotalTeamStats",
                    "headers": [
                        "CITY",
                        "NICKNAME",
                        "TEAM_ID",
                        "W",
                        "L",
                        "W_HOME",
                        "L_HOME",
                        "W_ROAD",
                        "L_ROAD",
                        "TEAM_TURNOVERS",
                        "TEAM_REBOUNDS",
                        "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",
                        "STL",
                        "TOTAL_TURNOVERS",
                        "BLK",
                        "PTS",
                        "AVG_REB",
                        "AVG_PTS",
                        "DQ",
                    ],
                    "rowSet": [
                        [
                            "Houston",
                            "Rockets",
                            100,
                            1,
                            0,
                            1,
                            0,
                            0,
                            0,
                            1,
                            10,
                            1,
                            5,
                            240,
                            0,
                            40,
                            85,
                            0.471,
                            10,
                            25,
                            0.4,
                            15,
                            20,
                            0.75,
                            8,
                            30,
                            48,
                            25,
                            15,
                            8,
                            12,
                            5,
                            105,
                            48.0,
                            105.0,
                            0,
                        ],
                    ],
                },
            ],
        }

        response = CumeStatsTeamResponse.model_validate(data)

        # Check player stats
        assert len(response.game_by_game_stats) == 2
        assert response.game_by_game_stats[0].player == "Player A"
        assert response.game_by_game_stats[1].player == "Player B"

        # Check team totals
        assert response.total_team_stats is not None
        assert response.total_team_stats.city == "Houston"
        assert response.total_team_stats.nickname == "Rockets"
        assert response.total_team_stats.pts == 105

    def test_handles_empty_player_stats(self):
        """CumeStatsTeamResponse handles empty GameByGameStats."""
        data = {
            "resultSets": [
                {
                    "name": "GameByGameStats",
                    "headers": [
                        "JERSEY_NUM",
                        "PLAYER",
                        "PERSON_ID",
                        "TEAM_ID",
                        "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_BLKP",
                        "MAX_PTS",
                        "AVG_ACTUAL_MINUTES",
                        "AVG_ACTUAL_SECONDS",
                        "AVG_REB",
                        "AVG_AST",
                        "AVG_STL",
                        "AVG_TURNOVERS",
                        "AVG_BLKP",
                        "AVG_PTS",
                        "PER_MIN_REB",
                        "PER_MIN_AST",
                        "PER_MIN_STL",
                        "PER_MIN_TURNOVERS",
                        "PER_MIN_BLK",
                        "PER_MIN_PTS",
                    ],
                    "rowSet": [],
                },
                {
                    "name": "TotalTeamStats",
                    "headers": [
                        "CITY",
                        "NICKNAME",
                        "TEAM_ID",
                        "W",
                        "L",
                        "W_HOME",
                        "L_HOME",
                        "W_ROAD",
                        "L_ROAD",
                        "TEAM_TURNOVERS",
                        "TEAM_REBOUNDS",
                        "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",
                        "STL",
                        "TOTAL_TURNOVERS",
                        "BLK",
                        "PTS",
                        "AVG_REB",
                        "AVG_PTS",
                        "DQ",
                    ],
                    "rowSet": [
                        [
                            "OPPONENTS",
                            " ",
                            0,
                            1,
                            1,
                            0,
                            1,
                            1,
                            0,
                            1,
                            23,
                            2,
                            10,
                            480,
                            0,
                            81,
                            170,
                            0.476,
                            25,
                            62,
                            0.403,
                            35,
                            51,
                            0.686,
                            15,
                            67,
                            105,
                            51,
                            39,
                            9,
                            18,
                            9,
                            222,
                            52.5,
                            111.0,
                            0,
                        ],
                    ],
                },
            ],
        }

        response = CumeStatsTeamResponse.model_validate(data)

        assert len(response.game_by_game_stats) == 0
        assert response.total_team_stats is not None
        assert response.total_team_stats.city == "OPPONENTS"

    def test_handles_empty_team_stats(self):
        """CumeStatsTeamResponse handles empty TotalTeamStats."""
        data = {
            "resultSets": [
                {
                    "name": "GameByGameStats",
                    "headers": [
                        "JERSEY_NUM",
                        "PLAYER",
                        "PERSON_ID",
                        "TEAM_ID",
                        "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_BLKP",
                        "MAX_PTS",
                        "AVG_ACTUAL_MINUTES",
                        "AVG_ACTUAL_SECONDS",
                        "AVG_REB",
                        "AVG_AST",
                        "AVG_STL",
                        "AVG_TURNOVERS",
                        "AVG_BLKP",
                        "AVG_PTS",
                        "PER_MIN_REB",
                        "PER_MIN_AST",
                        "PER_MIN_STL",
                        "PER_MIN_TURNOVERS",
                        "PER_MIN_BLK",
                        "PER_MIN_PTS",
                    ],
                    "rowSet": [],
                },
                {
                    "name": "TotalTeamStats",
                    "headers": [
                        "CITY",
                        "NICKNAME",
                        "TEAM_ID",
                        "W",
                        "L",
                        "W_HOME",
                        "L_HOME",
                        "W_ROAD",
                        "L_ROAD",
                        "TEAM_TURNOVERS",
                        "TEAM_REBOUNDS",
                        "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",
                        "STL",
                        "TOTAL_TURNOVERS",
                        "BLK",
                        "PTS",
                        "AVG_REB",
                        "AVG_PTS",
                        "DQ",
                    ],
                    "rowSet": [],
                },
            ],
        }

        response = CumeStatsTeamResponse.model_validate(data)

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