/**
 * Cap a billing anchor day to 28 (avoids issues with Feb and short months).
 */
export function capBillingDay(day: number): number {
  return Math.min(Math.max(1, Math.floor(day)), 28)
}

/**
 * Get today's date as YYYY-MM-DD string.
 */
export function todayISO(): string {
  return new Date().toISOString().split('T')[0]
}

/**
 * Check if a date of birth makes someone a minor (under 18).
 */
export function isMinor(dateOfBirth: Date | string): boolean {
  const dob = typeof dateOfBirth === 'string' ? new Date(dateOfBirth) : dateOfBirth
  const today = new Date()
  const age = today.getFullYear() - dob.getFullYear()
  const monthDiff = today.getMonth() - dob.getMonth()
  if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < dob.getDate())) {
    return age - 1 < 18
  }
  return age < 18
}
