"""Tests for Pydantic model parsing."""

from __future__ import annotations

from football_api.models import (
    AreaListResponse,
    AreaResponse,
    CompetitionResponse,
    Head2HeadResponse,
    Match,
    MatchListResponse,
    PersonResponse,
    ScorersResponse,
    StandingsResponse,
    TeamListResponse,
    TeamResponse,
)


class TestAreaModels:
    def test_area_response(self) -> None:
        data = {
            "id": 2077,
            "name": "Europe",
            "code": "EUR",
            "flag": None,
            "parentAreaId": 2267,
            "parentArea": "World",
            "childAreas": [
                {
                    "id": 2002,
                    "name": "Albania",
                    "countryCode": "ALB",
                    "flag": "https://crests.football-data.org/ALB.svg",
                    "parentAreaId": 2077,
                    "parentArea": "Europe",
                }
            ],
        }
        area = AreaResponse.model_validate(data)
        assert area.id == 2077
        assert area.name == "Europe"
        assert len(area.child_areas) == 1
        assert area.child_areas[0].name == "Albania"

    def test_area_list_response(self) -> None:
        data = {
            "count": 2,
            "areas": [
                {"id": 1, "name": "World", "code": "WLD"},
                {"id": 2, "name": "Europe", "code": "EUR"},
            ],
        }
        resp = AreaListResponse.model_validate(data)
        assert resp.count == 2
        assert len(resp.areas) == 2


class TestCompetitionModels:
    def test_competition_response(self) -> None:
        data = {
            "id": 2021,
            "name": "Premier League",
            "code": "PL",
            "type": "LEAGUE",
            "emblem": "https://crests.football-data.org/PL.png",
            "area": {"id": 2072, "name": "England", "code": "ENG"},
            "currentSeason": {
                "id": 1564,
                "startDate": "2023-08-11",
                "endDate": "2024-05-19",
                "currentMatchday": 20,
                "winner": None,
            },
            "seasons": [],
            "lastUpdated": "2024-01-01T00:00:00Z",
        }
        comp = CompetitionResponse.model_validate(data)
        assert comp.code == "PL"
        assert comp.current_season is not None
        assert comp.current_season.current_matchday == 20


class TestTeamModels:
    def test_team_response(self) -> None:
        data = {
            "id": 57,
            "name": "Arsenal FC",
            "shortName": "Arsenal",
            "tla": "ARS",
            "crest": "https://crests.football-data.org/57.png",
            "area": {"id": 2072, "name": "England", "code": "ENG"},
            "address": "75 Drayton Park London N5 1BU",
            "website": "http://www.arsenal.com",
            "founded": 1886,
            "clubColors": "Red / White",
            "venue": "Emirates Stadium",
            "coach": {"id": 1, "name": "Mikel Arteta"},
            "squad": [
                {
                    "id": 100,
                    "name": "Bukayo Saka",
                    "position": "Offence",
                    "shirtNumber": 7,
                }
            ],
            "runningCompetitions": [],
            "lastUpdated": "2024-01-01T00:00:00Z",
        }
        team = TeamResponse.model_validate(data)
        assert team.name == "Arsenal FC"
        assert team.tla == "ARS"
        assert len(team.squad) == 1
        assert team.squad[0].name == "Bukayo Saka"

    def test_team_list_response(self) -> None:
        data = {
            "count": 1,
            "competition": {"id": 2021, "name": "Premier League"},
            "season": {"id": 1564, "startDate": "2023-08-11", "endDate": "2024-05-19"},
            "teams": [
                {"id": 57, "name": "Arsenal FC", "tla": "ARS"},
            ],
        }
        resp = TeamListResponse.model_validate(data)
        assert resp.count == 1
        assert resp.teams[0].tla == "ARS"


class TestMatchModels:
    def test_match(self) -> None:
        data = {
            "id": 12345,
            "area": {"id": 2072, "name": "England"},
            "competition": {"id": 2021, "name": "Premier League"},
            "season": {"id": 1564, "startDate": "2023-08-11", "endDate": "2024-05-19"},
            "utcDate": "2024-01-01T15:00:00Z",
            "status": "FINISHED",
            "matchday": 20,
            "stage": "REGULAR_SEASON",
            "homeTeam": {"id": 57, "name": "Arsenal FC", "tla": "ARS"},
            "awayTeam": {"id": 65, "name": "Manchester City FC", "tla": "MCI"},
            "score": {
                "winner": "HOME_TEAM",
                "duration": "REGULAR",
                "fullTime": {"home": 2, "away": 1},
                "halfTime": {"home": 1, "away": 0},
            },
            "goals": [],
            "referees": [
                {"id": 1, "name": "Michael Oliver", "type": "REFEREE", "nationality": "England"}
            ],
        }
        match = Match.model_validate(data)
        assert match.status == "FINISHED"
        assert match.home_team is not None
        assert match.home_team.name == "Arsenal FC"
        assert match.score is not None
        assert match.score.full_time is not None
        assert match.score.full_time.home == 2

    def test_match_list_response(self) -> None:
        data = {
            "count": 1,
            "matches": [
                {
                    "id": 12345,
                    "status": "SCHEDULED",
                    "utcDate": "2024-01-15T20:00:00Z",
                    "homeTeam": {"id": 57, "name": "Arsenal FC"},
                    "awayTeam": {"id": 65, "name": "Manchester City FC"},
                    "score": {"winner": None, "fullTime": {"home": None, "away": None}},
                }
            ],
        }
        resp = MatchListResponse.model_validate(data)
        assert resp.count == 1
        assert len(resp.matches) == 1

    def test_head2head_response(self) -> None:
        data = {
            "aggregates": {"numberOfMatches": 5},
            "matches": [
                {
                    "id": 1,
                    "status": "FINISHED",
                    "homeTeam": {"id": 57, "name": "Arsenal FC"},
                    "awayTeam": {"id": 65, "name": "Manchester City FC"},
                    "score": {"winner": "HOME_TEAM", "fullTime": {"home": 3, "away": 1}},
                }
            ],
        }
        resp = Head2HeadResponse.model_validate(data)
        assert resp.aggregates is not None
        assert len(resp.matches) == 1


class TestStandingsModels:
    def test_standings_response(self) -> None:
        data = {
            "competition": {"id": 2021, "name": "Premier League"},
            "season": {"id": 1564, "startDate": "2023-08-11", "endDate": "2024-05-19"},
            "standings": [
                {
                    "stage": "REGULAR_SEASON",
                    "type": "TOTAL",
                    "table": [
                        {
                            "position": 1,
                            "team": {"id": 57, "name": "Arsenal FC"},
                            "playedGames": 20,
                            "won": 15,
                            "draw": 3,
                            "lost": 2,
                            "points": 48,
                            "goalsFor": 45,
                            "goalsAgainst": 15,
                            "goalDifference": 30,
                        }
                    ],
                }
            ],
        }
        resp = StandingsResponse.model_validate(data)
        assert len(resp.standings) == 1
        assert resp.standings[0].table[0].position == 1
        assert resp.standings[0].table[0].points == 48


class TestScorersModels:
    def test_scorers_response(self) -> None:
        data = {
            "count": 1,
            "competition": {"id": 2021, "name": "Premier League"},
            "season": {"id": 1564},
            "scorers": [
                {
                    "player": {"id": 200, "name": "Erling Haaland"},
                    "team": {"id": 65, "name": "Manchester City FC"},
                    "goals": 15,
                    "assists": 3,
                    "penalties": 2,
                }
            ],
        }
        resp = ScorersResponse.model_validate(data)
        assert len(resp.scorers) == 1
        assert resp.scorers[0].player is not None
        assert resp.scorers[0].goals == 15


class TestPersonModels:
    def test_person_response(self) -> None:
        data = {
            "id": 200,
            "name": "Erling Haaland",
            "firstName": "Erling",
            "lastName": "Haaland",
            "dateOfBirth": "2000-07-21",
            "nationality": "Norway",
            "position": "Centre-Forward",
            "shirtNumber": 9,
            "currentTeam": {
                "id": 65,
                "name": "Manchester City FC",
                "tla": "MCI",
            },
            "lastUpdated": "2024-01-01T00:00:00Z",
        }
        person = PersonResponse.model_validate(data)
        assert person.name == "Erling Haaland"
        assert person.current_team is not None
        assert person.current_team.tla == "MCI"
