"""Tests for MLS scraper."""

from datetime import datetime
from unittest.mock import patch

import pytest

from sportstime_parser.scrapers.mls import MLSScraper, create_mls_scraper
from sportstime_parser.scrapers.base import RawGameData
from sportstime_parser.tests.fixtures import (
    load_json_fixture,
    MLS_ESPN_SCOREBOARD_JSON,
)


class TestMLSScraperInit:
    """Test MLSScraper initialization."""

    def test_creates_scraper_with_season(self):
        """Test scraper initializes with correct season."""
        scraper = MLSScraper(season=2026)
        assert scraper.sport == "mls"
        assert scraper.season == 2026

    def test_factory_function_creates_scraper(self):
        """Test factory function creates correct scraper."""
        scraper = create_mls_scraper(season=2026)
        assert isinstance(scraper, MLSScraper)
        assert scraper.season == 2026

    def test_expected_game_count(self):
        """Test expected game count is correct for MLS."""
        scraper = MLSScraper(season=2026)
        assert scraper.expected_game_count == 493

    def test_sources_in_priority_order(self):
        """Test sources are returned in correct priority order."""
        scraper = MLSScraper(season=2026)
        sources = scraper._get_sources()
        assert sources == ["espn", "fbref"]


class TestESPNParsing:
    """Test ESPN API response parsing."""

    def test_parses_completed_games(self):
        """Test parsing completed games from ESPN."""
        scraper = MLSScraper(season=2026)
        data = load_json_fixture(MLS_ESPN_SCOREBOARD_JSON)
        games = scraper._parse_espn_response(data, "http://espn.com/api")

        completed = [g for g in games if g.status == "final"]
        assert len(completed) == 2

        # Galaxy @ LAFC
        la_lafc = next(g for g in completed if g.away_team_raw == "LA Galaxy")
        assert la_lafc.home_team_raw == "Los Angeles FC"
        assert la_lafc.away_score == 2
        assert la_lafc.home_score == 3
        assert la_lafc.stadium_raw == "BMO Stadium"

    def test_parses_scheduled_games(self):
        """Test parsing scheduled games from ESPN."""
        scraper = MLSScraper(season=2026)
        data = load_json_fixture(MLS_ESPN_SCOREBOARD_JSON)
        games = scraper._parse_espn_response(data, "http://espn.com/api")

        scheduled = [g for g in games if g.status == "scheduled"]
        assert len(scheduled) == 1

        ny_atl = scheduled[0]
        assert ny_atl.away_team_raw == "New York Red Bulls"
        assert ny_atl.home_team_raw == "Atlanta United FC"
        assert ny_atl.stadium_raw == "Mercedes-Benz Stadium"

    def test_parses_venue_info(self):
        """Test venue information is extracted."""
        scraper = MLSScraper(season=2026)
        data = load_json_fixture(MLS_ESPN_SCOREBOARD_JSON)
        games = scraper._parse_espn_response(data, "http://espn.com/api")

        for game in games:
            assert game.stadium_raw is not None


class TestGameNormalization:
    """Test game normalization and canonical ID generation."""

    def test_normalizes_games_with_canonical_ids(self):
        """Test games are normalized with correct canonical IDs."""
        scraper = MLSScraper(season=2026)

        raw_games = [
            RawGameData(
                game_date=datetime(2026, 3, 15),
                home_team_raw="Los Angeles FC",
                away_team_raw="LA Galaxy",
                stadium_raw="BMO Stadium",
                home_score=3,
                away_score=2,
                status="final",
                source_url="http://example.com",
            )
        ]

        games, review_items = scraper._normalize_games(raw_games)

        assert len(games) == 1
        game = games[0]

        # Check canonical ID format
        assert game.id == "mls_2026_lag_lafc_0315"
        assert game.sport == "mls"
        assert game.season == 2026

        # Check team IDs
        assert game.home_team_id == "team_mls_lafc"
        assert game.away_team_id == "team_mls_lag"

        # Check scores preserved
        assert game.home_score == 3
        assert game.away_score == 2

    def test_creates_review_items_for_unresolved_teams(self):
        """Test review items are created for unresolved teams."""
        scraper = MLSScraper(season=2026)

        raw_games = [
            RawGameData(
                game_date=datetime(2026, 3, 15),
                home_team_raw="Unknown Team XYZ",
                away_team_raw="LA Galaxy",
                stadium_raw="BMO Stadium",
                status="scheduled",
            ),
        ]

        games, review_items = scraper._normalize_games(raw_games)

        # Game should not be created due to unresolved team
        assert len(games) == 0

        # But there should be a review item
        assert len(review_items) >= 1


class TestTeamAndStadiumScraping:
    """Test team and stadium data scraping."""

    def test_scrapes_all_mls_teams(self):
        """Test all MLS teams are returned."""
        scraper = MLSScraper(season=2026)
        teams = scraper.scrape_teams()

        # MLS has 29+ teams
        assert len(teams) >= 29

        # Check team IDs are unique
        team_ids = [t.id for t in teams]
        assert len(set(team_ids)) == len(teams)

        # Check all teams have required fields
        for team in teams:
            assert team.id.startswith("team_mls_")
            assert team.sport == "mls"
            assert team.city
            assert team.name
            assert team.full_name
            assert team.abbreviation

    def test_teams_have_conferences(self):
        """Test teams have conference info."""
        scraper = MLSScraper(season=2026)
        teams = scraper.scrape_teams()

        # Count teams by conference
        eastern = [t for t in teams if t.conference == "Eastern"]
        western = [t for t in teams if t.conference == "Western"]

        # MLS has two conferences
        assert len(eastern) >= 14
        assert len(western) >= 14

    def test_scrapes_all_mls_stadiums(self):
        """Test all MLS stadiums are returned."""
        scraper = MLSScraper(season=2026)
        stadiums = scraper.scrape_stadiums()

        # Should have stadiums for all teams
        assert len(stadiums) >= 29

        # Check all stadiums have required fields
        for stadium in stadiums:
            assert stadium.id.startswith("stadium_mls_")
            assert stadium.sport == "mls"
            assert stadium.name
            assert stadium.city
            assert stadium.state
            assert stadium.country in ["USA", "Canada"]
            assert stadium.latitude != 0
            assert stadium.longitude != 0


class TestScrapeFallback:
    """Test multi-source fallback behavior."""

    def test_falls_back_to_next_source_on_failure(self):
        """Test scraper tries next source when first fails."""
        scraper = MLSScraper(season=2026)

        with patch.object(scraper, '_scrape_espn') as mock_espn, \
             patch.object(scraper, '_scrape_fbref') as mock_fbref:

            # Make ESPN fail
            mock_espn.side_effect = Exception("Connection failed")

            # Make FBref return data
            mock_fbref.return_value = [
                RawGameData(
                    game_date=datetime(2026, 3, 15),
                    home_team_raw="Los Angeles FC",
                    away_team_raw="LA Galaxy",
                    stadium_raw="BMO Stadium",
                    status="scheduled",
                )
            ]

            result = scraper.scrape_games()

            assert result.success
            assert result.source == "fbref"
            assert mock_espn.called
            assert mock_fbref.called


class TestSeasonMonths:
    """Test season month calculation."""

    def test_gets_correct_season_months(self):
        """Test correct months are returned for MLS season."""
        scraper = MLSScraper(season=2026)
        months = scraper._get_season_months()

        # MLS season is February-November
        assert len(months) == 10  # Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov

        # Check first month is February of season year
        assert months[0] == (2026, 2)

        # Check last month is November
        assert months[-1] == (2026, 11)
