from __future__ import annotations

import unittest

from hockey_data.models import Game
from hockey_data.service import HockeyDataService
from hockey_data.standings_parser import parse_standings_xml


STANDINGS_XML = """<?xml version="1.0" encoding="UTF-8"?>
<Standings idtournament="1369" genDate="2026-07-16 12:00:00" order="pts">
  <League name="Общая таблица" name_en="League" in_playoff="2">
    <Team id="14927" clubid="1" name="Команда А" nameen="Team A" rank="1" gp="10" w="7" otw="1" sow="0" sol="1" otl="0" l="1" pts="17" pts_pct="85.0" gf="31" ga="18" playoffs_in="Y" />
    <Team id="14928" clubid="2" name="Команда Б" nameen="Team B" rank="2" gp="10" w="6" otw="0" sow="1" sol="0" otl="1" l="2" pts="15" pts_pct="75.0" gf="29" ga="22" />
  </League>
  <Conferences>
    <Conference item="west" name="Запад" name_en="West" in_playoff="1">
      <Team id="14927" clubid="1" name="Команда А" nameen="Team A" rank="1" gp="10" w="7" pts="17" gf="31" ga="18" />
    </Conference>
    <Conference item="east" name="Восток" name_en="East" in_playoff="1">
      <Team id="14928" clubid="2" name="Команда Б" nameen="Team B" rank="1" gp="10" w="6" pts="15" gf="29" ga="22" />
    </Conference>
  </Conferences>
  <Divisions>
    <Division item="bobrov" name="Дивизион Боброва" name_en="Bobrov Division" in_playoff="1" conference="west">
      <Team id="14927" clubid="1" name="Команда А" nameen="Team A" rank="1" gp="10" w="7" pts="17" gf="31" ga="18" />
    </Division>
    <Division item="tarasov" name="Дивизион Тарасова" name_en="Tarasov Division" in_playoff="1" conference="west">
      <Team id="14928" clubid="2" name="Команда Б" nameen="Team B" rank="1" gp="10" w="6" pts="15" gf="29" ga="22" />
    </Division>
  </Divisions>
</Standings>
"""


class StandingsParserTests(unittest.TestCase):
    def test_parses_all_standings_variants(self) -> None:
        result = parse_standings_xml(STANDINGS_XML, language="ru")

        self.assertEqual(result["tournament_id"], "1369")
        self.assertEqual(
            [variant["id"] for variant in result["variants"]],
            ["league", "conferences", "divisions"],
        )
        self.assertEqual(len(result["variants"][0]["groups"][0]["teams"]), 2)
        self.assertEqual(len(result["variants"][1]["groups"]), 2)
        self.assertEqual(len(result["variants"][2]["groups"]), 2)
        first = result["variants"][0]["groups"][0]["teams"][0]
        self.assertEqual(first["entry_id"], "14927")
        self.assertEqual(first["club_id"], "1")
        self.assertEqual(first["points"], 17)
        self.assertEqual(first["goals_for"], 31)

    def test_uses_english_names(self) -> None:
        result = parse_standings_xml(STANDINGS_XML, language="en")

        league = result["variants"][0]
        self.assertEqual(league["label"], "League")
        self.assertEqual(league["groups"][0]["name"], "League")
        self.assertEqual(league["groups"][0]["teams"][0]["name"], "Team A")

    def test_highlights_selected_game_teams_by_ids_and_names(self) -> None:
        result = parse_standings_xml(STANDINGS_XML, language="ru")
        game = Game(
            external_id="900001",
            tournament_external_id="1369",
            home_entry_external_id="14927",
            home_team_external_id="1",
            home_team_name_ru="Другое отображаемое имя",
            away_entry_external_id="",
            away_team_external_id="",
            away_team_name_ru="Команда Б",
        )

        HockeyDataService._highlight_standings_teams(result, game)
        teams = result["variants"][0]["groups"][0]["teams"]

        self.assertTrue(teams[0]["highlighted"])
        self.assertEqual(teams[0]["side"], "home")
        self.assertTrue(teams[1]["highlighted"])
        self.assertEqual(teams[1]["side"], "away")


if __name__ == "__main__":
    unittest.main()
