const API_URL = "https://rw.vestaboard.com/"

export const sendGrid = async (grid: number[][]): Promise<void> => {
  const apiKey = process.env.VESTABOARD_API_KEY
  if (!apiKey) {
    throw new Error("VESTABOARD_API_KEY not set in environment")
  }

  console.log(`🌭 sending`)
  const response = await fetch(API_URL, {
    method: "POST",
    headers: {
      "X-Vestaboard-Read-Write-Key": apiKey,
      "Content-Type": "application/json",
    },
    body: JSON.stringify(grid),
  })

  if (!response.ok) {
    const text = await response.text()
    console.log(`🌭 Error ${text}`)
    throw new Error(`Vestaboard API error (${response.status}): ${text}`)
  } else {
    console.log(`🌭 sent successfully`, await response.text())
  }
}
