from fastbreak.models import LeagueStandingsResponse, TeamStanding


class TestTeamStanding:
    """Tests for TeamStanding model."""

    def test_parse_valid_standing(self):
        """TeamStanding parses a valid row from the API."""
        data = {
            "LeagueID": "00",
            "SeasonID": "22025",
            "TeamID": 1610612760,
            "TeamCity": "Oklahoma City",
            "TeamName": "Thunder",
            "TeamSlug": "thunder",
            "Conference": "West",
            "ConferenceRecord": "28-6 ",
            "PlayoffRank": 1,
            "ClinchIndicator": "",
            "Division": "Northwest",
            "DivisionRecord": "7-2",
            "DivisionRank": 1,
            "WINS": 36,
            "LOSSES": 8,
            "WinPCT": 0.818,
            "LeagueRank": 1,
            "Record": "36-8",
            "HOME": "20-2",
            "ROAD": "16-5",
            "L10": "7-3",
            "Last10Home": "8-2",
            "Last10Road": "6-4",
            "OT": "3-0",
            "ThreePTSOrLess": "2-4",
            "TenPTSOrMore": "27-3",
            "LongHomeStreak": 14,
            "strLongHomeStreak": "W 14",
            "LongRoadStreak": 8,
            "strLongRoadStreak": "W 8",
            "LongWinStreak": 16,
            "LongLossStreak": 2,
            "CurrentHomeStreak": 3,
            "strCurrentHomeStreak": "W 3",
            "CurrentRoadStreak": 1,
            "strCurrentRoadStreak": "W 1",
            "CurrentStreak": 1,
            "strCurrentStreak": "W 1",
            "ConferenceGamesBack": 0.0,
            "DivisionGamesBack": 0.0,
            "ClinchedConferenceTitle": None,
            "ClinchedDivisionTitle": None,
            "ClinchedPlayoffBirth": None,
            "ClinchedPlayIn": 0,
            "EliminatedConference": None,
            "EliminatedDivision": None,
            "AheadAtHalf": "27-6",
            "BehindAtHalf": "9-2",
            "TiedAtHalf": "0-0",
            "AheadAtThird": "32-2",
            "BehindAtThird": "4-5",
            "TiedAtThird": "0-1",
            "Score100PTS": "36-7 ",
            "OppScore100PTS": "26-8 ",
            "OppOver500": "16-7 ",
            "LeadInFGPCT": "31-4",
            "LeadInReb": "17-2",
            "FewerTurnovers": "34-6 ",
            "PointsPG": 121.4,
            "OppPointsPG": 108.0,
            "DiffPointsPG": 13.5,
            "vsEast": "8-2",
            "vsAtlantic": "1-0",
            "vsCentral": "2-0",
            "vsSoutheast": "5-2",
            "vsWest": "28-6",
            "vsNorthwest": "7-2",
            "vsPacific": "11-1",
            "vsSouthwest": "10-3",
            "Jan": "7-3",
            "Feb": "0-0",
            "Mar": "0-0",
            "Apr": "0-0",
            "May": "0-0",
            "Jun": "0-0",
            "Jul": "0-0",
            "Aug": "0-0",
            "Sep": "0-0",
            "Oct": "6-0",
            "Nov": "14-1",
            "Dec": "9-4",
            "Score_80_Plus": "36-8 ",
            "Opp_Score_80_Plus": "36-8 ",
            "Score_Below_80": "0-0",
            "Opp_Score_Below_80": "0-0",
            "TotalPoints": 5342,
            "OppTotalPoints": 4750,
            "DiffTotalPoints": 592,
            "LeagueGamesBack": 0.0,
            "PlayoffSeeding": None,
            "ClinchedPostSeason": 0,
            "NEUTRAL": "0-1",
        }

        standing = TeamStanding.model_validate(data)

        assert standing.team_id == 1610612760
        assert standing.team_city == "Oklahoma City"
        assert standing.team_name == "Thunder"
        assert standing.wins == 36
        assert standing.losses == 8
        assert standing.win_pct == 0.818
        assert standing.conference == "West"
        assert standing.division == "Northwest"
        assert standing.playoff_rank == 1

    def test_nullable_fields_handle_none(self):
        """TeamStanding handles null values for optional fields."""
        data = {
            "LeagueID": "00",
            "SeasonID": "22025",
            "TeamID": 1610612765,
            "TeamCity": "Detroit",
            "TeamName": "Pistons",
            "TeamSlug": "pistons",
            "Conference": "East",
            "ConferenceRecord": "22-6 ",
            "PlayoffRank": 1,
            "ClinchIndicator": "",
            "Division": "Central",
            "DivisionRecord": "8-3",
            "DivisionRank": 1,
            "WINS": 31,
            "LOSSES": 10,
            "WinPCT": 0.756,
            "LeagueRank": None,  # Null in API
            "Record": "31-10",
            "HOME": "17-4",
            "ROAD": "13-6",
            "L10": "7-3",
            "Last10Home": "8-2",
            "Last10Road": "6-4",
            "OT": "1-1",
            "ThreePTSOrLess": "6-4",
            "TenPTSOrMore": "14-2",
            "LongHomeStreak": 6,
            "strLongHomeStreak": "W 6",
            "LongRoadStreak": 7,
            "strLongRoadStreak": "W 7",
            "LongWinStreak": 13,
            "LongLossStreak": 2,
            "CurrentHomeStreak": 3,
            "strCurrentHomeStreak": "W 3",
            "CurrentRoadStreak": 2,
            "strCurrentRoadStreak": "W 2",
            "CurrentStreak": 3,
            "strCurrentStreak": "W 3",
            "ConferenceGamesBack": 0.0,
            "DivisionGamesBack": 0.0,
            "ClinchedConferenceTitle": None,
            "ClinchedDivisionTitle": None,
            "ClinchedPlayoffBirth": None,
            "ClinchedPlayIn": 0,
            "EliminatedConference": None,
            "EliminatedDivision": None,
            "AheadAtHalf": "26-4",
            "BehindAtHalf": "5-6",
            "TiedAtHalf": "0-0",
            "AheadAtThird": "25-2",
            "BehindAtThird": "6-8",
            "TiedAtThird": "0-0",
            "Score100PTS": "30-7 ",
            "OppScore100PTS": "26-9 ",
            "OppOver500": "14-5 ",
            "LeadInFGPCT": "26-3",
            "LeadInReb": "21-6",
            "FewerTurnovers": "22-4 ",
            "PointsPG": 117.3,
            "OppPointsPG": 110.1,
            "DiffPointsPG": 7.3,
            "vsEast": "22-6",
            "vsAtlantic": "7-1",
            "vsCentral": "8-3",
            "vsSoutheast": "7-2",
            "vsWest": "9-4",
            "vsNorthwest": "3-1",
            "vsPacific": "3-2",
            "vsSouthwest": "3-1",
            "Jan": "6-2",
            "Feb": "0-0",
            "Mar": "0-0",
            "Apr": "0-0",
            "May": "0-0",
            "Jun": "0-0",
            "Jul": "0-0",
            "Aug": "0-0",
            "Sep": "0-0",
            "Oct": "3-2",
            "Nov": "13-2",
            "Dec": "9-4",
            "Score_80_Plus": "31-10",
            "Opp_Score_80_Plus": "30-10",
            "Score_Below_80": "0-0",
            "Opp_Score_Below_80": "1-0",
            "TotalPoints": 4811,
            "OppTotalPoints": 4513,
            "DiffTotalPoints": 298,
            "LeagueGamesBack": 3.5,
            "PlayoffSeeding": None,
            "ClinchedPostSeason": 0,
            "NEUTRAL": "1-0",
        }

        standing = TeamStanding.model_validate(data)

        assert standing.league_rank is None
        assert standing.clinched_conference_title is None
        assert standing.playoff_seeding is None


class TestLeagueStandingsResponse:
    """Tests for LeagueStandingsResponse model."""

    def test_parse_result_sets_format(self):
        """LeagueStandingsResponse transforms tabular resultSets format."""
        data = {
            "resource": "leaguestandings",
            "parameters": {
                "LeagueID": "00",
                "SeasonYear": "2025-26",
                "SeasonType": "Regular Season",
            },
            "resultSets": [
                {
                    "name": "Standings",
                    "headers": [
                        "LeagueID",
                        "SeasonID",
                        "TeamID",
                        "TeamCity",
                        "TeamName",
                        "TeamSlug",
                        "Conference",
                        "ConferenceRecord",
                        "PlayoffRank",
                        "ClinchIndicator",
                        "Division",
                        "DivisionRecord",
                        "DivisionRank",
                        "WINS",
                        "LOSSES",
                        "WinPCT",
                        "LeagueRank",
                        "Record",
                        "HOME",
                        "ROAD",
                        "L10",
                        "Last10Home",
                        "Last10Road",
                        "OT",
                        "ThreePTSOrLess",
                        "TenPTSOrMore",
                        "LongHomeStreak",
                        "strLongHomeStreak",
                        "LongRoadStreak",
                        "strLongRoadStreak",
                        "LongWinStreak",
                        "LongLossStreak",
                        "CurrentHomeStreak",
                        "strCurrentHomeStreak",
                        "CurrentRoadStreak",
                        "strCurrentRoadStreak",
                        "CurrentStreak",
                        "strCurrentStreak",
                        "ConferenceGamesBack",
                        "DivisionGamesBack",
                        "ClinchedConferenceTitle",
                        "ClinchedDivisionTitle",
                        "ClinchedPlayoffBirth",
                        "ClinchedPlayIn",
                        "EliminatedConference",
                        "EliminatedDivision",
                        "AheadAtHalf",
                        "BehindAtHalf",
                        "TiedAtHalf",
                        "AheadAtThird",
                        "BehindAtThird",
                        "TiedAtThird",
                        "Score100PTS",
                        "OppScore100PTS",
                        "OppOver500",
                        "LeadInFGPCT",
                        "LeadInReb",
                        "FewerTurnovers",
                        "PointsPG",
                        "OppPointsPG",
                        "DiffPointsPG",
                        "vsEast",
                        "vsAtlantic",
                        "vsCentral",
                        "vsSoutheast",
                        "vsWest",
                        "vsNorthwest",
                        "vsPacific",
                        "vsSouthwest",
                        "Jan",
                        "Feb",
                        "Mar",
                        "Apr",
                        "May",
                        "Jun",
                        "Jul",
                        "Aug",
                        "Sep",
                        "Oct",
                        "Nov",
                        "Dec",
                        "Score_80_Plus",
                        "Opp_Score_80_Plus",
                        "Score_Below_80",
                        "Opp_Score_Below_80",
                        "TotalPoints",
                        "OppTotalPoints",
                        "DiffTotalPoints",
                        "LeagueGamesBack",
                        "PlayoffSeeding",
                        "ClinchedPostSeason",
                        "NEUTRAL",
                    ],
                    "rowSet": [
                        [
                            "00",
                            "22025",
                            1610612760,
                            "Oklahoma City",
                            "Thunder",
                            "thunder",
                            "West",
                            "28-6 ",
                            1,
                            "",
                            "Northwest",
                            "7-2",
                            1,
                            36,
                            8,
                            0.818,
                            1,
                            "36-8",
                            "20-2",
                            "16-5",
                            "7-3",
                            "8-2",
                            "6-4",
                            "3-0",
                            "2-4",
                            "27-3",
                            14,
                            "W 14",
                            8,
                            "W 8",
                            16,
                            2,
                            3,
                            "W 3",
                            1,
                            "W 1",
                            1,
                            "W 1",
                            0.0,
                            0.0,
                            None,
                            None,
                            None,
                            0,
                            None,
                            None,
                            "27-6",
                            "9-2",
                            "0-0",
                            "32-2",
                            "4-5",
                            "0-1",
                            "36-7 ",
                            "26-8 ",
                            "16-7 ",
                            "31-4",
                            "17-2",
                            "34-6 ",
                            121.4,
                            108.0,
                            13.5,
                            "8-2",
                            "1-0",
                            "2-0",
                            "5-2",
                            "28-6",
                            "7-2",
                            "11-1",
                            "10-3",
                            "7-3",
                            "0-0",
                            "0-0",
                            "0-0",
                            "0-0",
                            "0-0",
                            "0-0",
                            "0-0",
                            "0-0",
                            "6-0",
                            "14-1",
                            "9-4",
                            "36-8 ",
                            "36-8 ",
                            "0-0",
                            "0-0",
                            5342,
                            4750,
                            592,
                            0.0,
                            None,
                            0,
                            "0-1",
                        ],
                        [
                            "00",
                            "22025",
                            1610612765,
                            "Detroit",
                            "Pistons",
                            "pistons",
                            "East",
                            "22-6 ",
                            1,
                            "",
                            "Central",
                            "8-3",
                            1,
                            31,
                            10,
                            0.756,
                            None,
                            "31-10",
                            "17-4",
                            "13-6",
                            "7-3",
                            "8-2",
                            "6-4",
                            "1-1",
                            "6-4",
                            "14-2",
                            6,
                            "W 6",
                            7,
                            "W 7",
                            13,
                            2,
                            3,
                            "W 3",
                            2,
                            "W 2",
                            3,
                            "W 3",
                            0.0,
                            0.0,
                            None,
                            None,
                            None,
                            0,
                            None,
                            None,
                            "26-4",
                            "5-6",
                            "0-0",
                            "25-2",
                            "6-8",
                            "0-0",
                            "30-7 ",
                            "26-9 ",
                            "14-5 ",
                            "26-3",
                            "21-6",
                            "22-4 ",
                            117.3,
                            110.1,
                            7.3,
                            "22-6",
                            "7-1",
                            "8-3",
                            "7-2",
                            "9-4",
                            "3-1",
                            "3-2",
                            "3-1",
                            "6-2",
                            "0-0",
                            "0-0",
                            "0-0",
                            "0-0",
                            "0-0",
                            "0-0",
                            "0-0",
                            "0-0",
                            "3-2",
                            "13-2",
                            "9-4",
                            "31-10",
                            "30-10",
                            "0-0",
                            "1-0",
                            4811,
                            4513,
                            298,
                            3.5,
                            None,
                            0,
                            "1-0",
                        ],
                    ],
                }
            ],
        }

        response = LeagueStandingsResponse.model_validate(data)

        assert len(response.standings) == 2
        assert response.standings[0].team_name == "Thunder"
        assert response.standings[0].wins == 36
        assert response.standings[1].team_name == "Pistons"
        assert response.standings[1].wins == 31

    def test_standings_sorted_by_api_order(self):
        """Standings maintain order from API response."""
        data = {
            "resultSets": [
                {
                    "name": "Standings",
                    "headers": [
                        "LeagueID",
                        "SeasonID",
                        "TeamID",
                        "TeamCity",
                        "TeamName",
                        "TeamSlug",
                        "Conference",
                        "ConferenceRecord",
                        "PlayoffRank",
                        "ClinchIndicator",
                        "Division",
                        "DivisionRecord",
                        "DivisionRank",
                        "WINS",
                        "LOSSES",
                        "WinPCT",
                        "LeagueRank",
                        "Record",
                        "HOME",
                        "ROAD",
                        "L10",
                        "Last10Home",
                        "Last10Road",
                        "OT",
                        "ThreePTSOrLess",
                        "TenPTSOrMore",
                        "LongHomeStreak",
                        "strLongHomeStreak",
                        "LongRoadStreak",
                        "strLongRoadStreak",
                        "LongWinStreak",
                        "LongLossStreak",
                        "CurrentHomeStreak",
                        "strCurrentHomeStreak",
                        "CurrentRoadStreak",
                        "strCurrentRoadStreak",
                        "CurrentStreak",
                        "strCurrentStreak",
                        "ConferenceGamesBack",
                        "DivisionGamesBack",
                        "ClinchedConferenceTitle",
                        "ClinchedDivisionTitle",
                        "ClinchedPlayoffBirth",
                        "ClinchedPlayIn",
                        "EliminatedConference",
                        "EliminatedDivision",
                        "AheadAtHalf",
                        "BehindAtHalf",
                        "TiedAtHalf",
                        "AheadAtThird",
                        "BehindAtThird",
                        "TiedAtThird",
                        "Score100PTS",
                        "OppScore100PTS",
                        "OppOver500",
                        "LeadInFGPCT",
                        "LeadInReb",
                        "FewerTurnovers",
                        "PointsPG",
                        "OppPointsPG",
                        "DiffPointsPG",
                        "vsEast",
                        "vsAtlantic",
                        "vsCentral",
                        "vsSoutheast",
                        "vsWest",
                        "vsNorthwest",
                        "vsPacific",
                        "vsSouthwest",
                        "Jan",
                        "Feb",
                        "Mar",
                        "Apr",
                        "May",
                        "Jun",
                        "Jul",
                        "Aug",
                        "Sep",
                        "Oct",
                        "Nov",
                        "Dec",
                        "Score_80_Plus",
                        "Opp_Score_80_Plus",
                        "Score_Below_80",
                        "Opp_Score_Below_80",
                        "TotalPoints",
                        "OppTotalPoints",
                        "DiffTotalPoints",
                        "LeagueGamesBack",
                        "PlayoffSeeding",
                        "ClinchedPostSeason",
                        "NEUTRAL",
                    ],
                    "rowSet": [
                        [
                            "00",
                            "22025",
                            1,
                            "First",
                            "Team",
                            "first",
                            "West",
                            "10-5",
                            1,
                            "",
                            "Northwest",
                            "5-2",
                            1,
                            20,
                            10,
                            0.667,
                            1,
                            "20-10",
                            "10-5",
                            "10-5",
                            "6-4",
                            "5-5",
                            "5-5",
                            "1-0",
                            "2-2",
                            "10-5",
                            5,
                            "W 5",
                            3,
                            "W 3",
                            5,
                            2,
                            2,
                            "W 2",
                            1,
                            "W 1",
                            2,
                            "W 2",
                            0.0,
                            0.0,
                            None,
                            None,
                            None,
                            0,
                            None,
                            None,
                            "15-5",
                            "5-5",
                            "0-0",
                            "15-5",
                            "5-5",
                            "0-0",
                            "20-8",
                            "18-10",
                            "10-5",
                            "15-5",
                            "12-8",
                            "15-5",
                            110.0,
                            105.0,
                            5.0,
                            "5-5",
                            "2-2",
                            "2-2",
                            "1-1",
                            "15-5",
                            "5-2",
                            "5-2",
                            "5-1",
                            "5-5",
                            "0-0",
                            "0-0",
                            "0-0",
                            "0-0",
                            "0-0",
                            "0-0",
                            "0-0",
                            "0-0",
                            "5-2",
                            "10-5",
                            "5-3",
                            "20-10",
                            "20-10",
                            "0-0",
                            "0-0",
                            3300,
                            3150,
                            150,
                            0.0,
                            None,
                            0,
                            "0-0",
                        ],
                        [
                            "00",
                            "22025",
                            2,
                            "Second",
                            "Team",
                            "second",
                            "West",
                            "9-6",
                            2,
                            "",
                            "Northwest",
                            "4-3",
                            2,
                            18,
                            12,
                            0.6,
                            2,
                            "18-12",
                            "9-6",
                            "9-6",
                            "5-5",
                            "5-5",
                            "5-5",
                            "0-1",
                            "3-3",
                            "8-6",
                            4,
                            "W 4",
                            3,
                            "W 3",
                            4,
                            3,
                            1,
                            "W 1",
                            -1,
                            "L 1",
                            1,
                            "W 1",
                            2.0,
                            2.0,
                            None,
                            None,
                            None,
                            0,
                            None,
                            None,
                            "12-6",
                            "6-6",
                            "0-0",
                            "12-6",
                            "6-6",
                            "0-0",
                            "18-10",
                            "16-12",
                            "8-6",
                            "12-6",
                            "10-8",
                            "12-6",
                            108.0,
                            106.0,
                            2.0,
                            "4-6",
                            "1-3",
                            "2-2",
                            "1-1",
                            "14-6",
                            "4-3",
                            "5-2",
                            "5-1",
                            "4-6",
                            "0-0",
                            "0-0",
                            "0-0",
                            "0-0",
                            "0-0",
                            "0-0",
                            "0-0",
                            "0-0",
                            "4-3",
                            "9-6",
                            "5-3",
                            "18-12",
                            "18-12",
                            "0-0",
                            "0-0",
                            3240,
                            3180,
                            60,
                            2.0,
                            None,
                            0,
                            "0-0",
                        ],
                    ],
                }
            ],
        }

        response = LeagueStandingsResponse.model_validate(data)

        assert response.standings[0].team_city == "First"
        assert response.standings[1].team_city == "Second"
