/**
 * UTC month boundary utilities — api-usage-quota-ui (wave-12).
 */

/**
 * Returns { start, end } ISO timestamps (inclusive) for the given UTC month.
 * @param month  "YYYY-MM" string; defaults to the current UTC month.
 */
export function currentUtcMonthBounds(month?: string): { start: string; end: string } {
  const now = new Date()
  const [year, mo] = month
    ? month.split('-').map(Number) as [number, number]
    : [now.getUTCFullYear(), now.getUTCMonth() + 1]

  const start = new Date(Date.UTC(year, mo - 1, 1, 0, 0, 0, 0))
  const end = new Date(Date.UTC(year, mo, 1, 0, 0, 0, 0)) // exclusive — start of next month

  return {
    start: start.toISOString(),
    end: end.toISOString(),
  }
}

/**
 * Returns a Date pointing at the first moment of the next UTC calendar month.
 * Used for the `Retry-After` header when quota is exceeded.
 */
export function nextUtcMonthStart(): Date {
  const now = new Date()
  return new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth() + 1, 1, 0, 0, 0, 0))
}
