import type { Team } from './apiResponseTypes'
import type { Player } from './playersService'
import { CURRENT_SEASON, SITE_URL } from './utilities'

export interface Transaction {
  id: number
  week: number
  player: Player
  oldteam: Team
  newteam: Team
  season: number
  moveid: number
  cancelled: boolean
  frozen: boolean
}

export async function fetchTransactionsByTeamAndWeek(seasonNumber: number, teamAbbreviation: string, weekNumber: number): Promise<Transaction[]> {
  const response = await fetch(`${SITE_URL}/api/v3/transactions?season=${seasonNumber}&team_abbrev=${teamAbbreviation}&week_start=${weekNumber}&week_end=${weekNumber}`)

  const transactionsResponse: {
    count: number
    transactions: Transaction[]
  } = await response.json()

  return transactionsResponse.transactions
}

export async function fetchTransactionsForCurrentSeasonByPlayerName(playerName: string): Promise<Transaction[]> {
  const response = await fetch(`${SITE_URL}/api/v3/transactions?season=${CURRENT_SEASON}&player_name=${playerName}`)

  const transactionsResponse: {
    count: number
    transactions: Transaction[]
  } = await response.json()

  return transactionsResponse.transactions
}
