export type DateRange = { startIso: string; endIso: string }

export function computeDateRange(period: string): DateRange {
  const now = new Date()
  const start = new Date(now.getTime())
  if (period === '7d') start.setDate(start.getDate() - 7)
  else if (period === '30d') start.setDate(start.getDate() - 30)
  else if (period === '90d') start.setDate(start.getDate() - 90)
  else if (period === '1y') start.setFullYear(start.getFullYear() - 1)
  return { startIso: start.toISOString(), endIso: now.toISOString() }
}

export function toDateOnly(iso: string): string {
  return iso.length >= 10 ? iso.substring(0, 10) : iso
}
