import type { Game } from './apiResponseTypes'
import { MODERN_STAT_ERA_START, SITE_URL } from './utilities'

export async function fetchGamesBySeasonAndTeamId(seasonNumber: number, teamId: number): Promise<Game[]> {
  if (seasonNumber < MODERN_STAT_ERA_START) {
    console.warn(`Cannot use games endpoint to fetch stats before season 8`)
    return []
  }

  const response = await fetch(`${SITE_URL}/api/v3/games?season=${seasonNumber}&team1_id=${teamId}&team2_id=${teamId}&season_type=regular`)

  const gamesResponse: {
    count: number
    games: Game[]
  } = await response.json()

  return gamesResponse.games
}
