"""Fetch team stats for individual games."""

# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/team.ipynb.

# %% auto 0
__all__ = ['fetch_all_teams', 'fetch_team_stats']

# %% ../nbs/team.ipynb 4
import pandas as pd
import typing

# %% ../nbs/team.ipynb 5
def fetch_all_teams():
    """
    Fetches all team available
    """
    try:
        print("Fetching all teams available...")
        team_url = "https://github.com/seanyboi/rugbydata/blob/main/data/v3/team/team_registry.parquet?raw=true"
        team_stats = pd.read_parquet(team_url, engine="pyarrow")
        return team_stats
    except Exception as _:
        print("No teams available. Please raise an issue!")

# %% ../nbs/team.ipynb 7
def fetch_team_stats(team_id: str, date: typing.Optional[str] = None):
    """
    Fetches all team stats for a particular team or if a date is passed (YYYYMMDD) then just for a particular game
    """
    try:
        if date:
            print(f"Fetching team stats for team_id: {team_id} on date: {date}...")
            team_url = f"https://github.com/seanyboi/rugbydata/blob/main/data/v3/team/{team_id}.parquet?raw=true"
            team_stats = pd.read_parquet(team_url, engine="pyarrow")
            team_stats = team_stats.query("game_date == @date")
            return team_stats
        else:
            print(f"Fetching all team stats for team_id: {team_id}...")
            team_url = f"https://github.com/seanyboi/rugbydata/blob/main/data/v3/team/{team_id}.parquet?raw=true"
            team_stats = pd.read_parquet(team_url, engine="pyarrow")
            return team_stats
    except Exception as _:
        print(
            "No team stats for {team_id} because the team id does not exist. Please raise an issue!"
        )
