"""
Betika odds scraper. Same approach as sportpesa.py - see that file's docstring
for how to find the real API endpoint via browser DevTools.
"""

import requests
from typing import List
from .base import BaseScraper, OddsEntry

# TODO: replace with the real endpoint once you've found it via DevTools
API_URL = "https://www.betika.com/api/TODO_REPLACE_ME"

HEADERS = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
    # TODO: add any auth/session headers the real request needs
}


class BetikaScraper(BaseScraper):
    name = "betika"

    def fetch_odds(self, sport: str = "football") -> List[OddsEntry]:
        if API_URL.endswith("TODO_REPLACE_ME"):
            return self._mock_odds(sport)

        resp = requests.get(API_URL, headers=HEADERS, timeout=10)
        resp.raise_for_status()
        data = resp.json()

        entries = []
        # TODO: adjust this parsing to match Betika's real JSON shape.
        for match in data.get("matches", []):
            home = match["homeTeam"]
            away = match["awayTeam"]
            kickoff = match["kickoffTime"]
            for outcome, odds_value in match.get("winnerOdds", {}).items():
                entries.append(OddsEntry(
                    bookmaker=self.name,
                    sport=sport,
                    home_team=home,
                    away_team=away,
                    kickoff_utc=kickoff,
                    market="match_winner",
                    outcome=outcome,
                    odds=float(odds_value),
                ))
        return entries

    def _mock_odds(self, sport: str) -> List[OddsEntry]:
        """Fake data - deliberately set up to produce an arb against the SportPesa mock data."""
        return [
            OddsEntry(self.name, sport, "Gor Mahia", "AFC Leopards",
                      "2026-08-23T15:00:00Z", "match_winner", "home", 2.10),
            OddsEntry(self.name, sport, "Gor Mahia", "AFC Leopards",
                      "2026-08-23T15:00:00Z", "match_winner", "draw", 3.05),
            OddsEntry(self.name, sport, "Gor Mahia", "AFC Leopards",
                      "2026-08-23T15:00:00Z", "match_winner", "away", 4.20),
        ]
