/**
 * Week summary helpers — time-management.
 *
 * Client-side / server-side utilities for the /time page week view.
 */

/**
 * Format a week number to a human-readable label.
 * E.g. '2026-W23' → 'Jun 1 – Jun 7'
 */
export function formatWeekLabel(week: string, locale: 'he' | 'en'): string {
  const m = /^(\d{4})-W(\d{2})$/.exec(week)
  if (!m) return week

  const year = parseInt(m[1] ?? '0', 10)
  const weekNum = parseInt(m[2] ?? '0', 10)

  // Jan 4 is always in week 1
  const jan4 = new Date(Date.UTC(year, 0, 4))
  const dayOfWeek = jan4.getUTCDay() || 7
  const monday = new Date(jan4)
  monday.setUTCDate(jan4.getUTCDate() - (dayOfWeek - 1) + (weekNum - 1) * 7)
  const sunday = new Date(monday)
  sunday.setUTCDate(monday.getUTCDate() + 6)

  const tag = locale === 'he' ? 'he-IL' : 'en-IL'
  const fmt = new Intl.DateTimeFormat(tag, { month: 'short', day: 'numeric', timeZone: 'UTC' })
  return `${fmt.format(monday)} – ${fmt.format(sunday)}`
}

/**
 * Get the current ISO week string 'YYYY-WNN' in UTC.
 */
export function getCurrentWeek(): string {
  const now = new Date()
  const jan4 = new Date(Date.UTC(now.getUTCFullYear(), 0, 4))
  const dayOfWeek = jan4.getUTCDay() || 7
  const firstMonday = new Date(jan4)
  firstMonday.setUTCDate(jan4.getUTCDate() - (dayOfWeek - 1))

  const monday = new Date(now)
  const dow = monday.getUTCDay() || 7
  monday.setUTCDate(monday.getUTCDate() - (dow - 1))

  const weekNum = Math.round((monday.getTime() - firstMonday.getTime()) / (7 * 86400 * 1000)) + 1
  const weekStr = String(weekNum).padStart(2, '0')
  return `${now.getUTCFullYear()}-W${weekStr}`
}

/**
 * Navigate to previous/next week.
 */
export function adjustWeek(week: string, delta: -1 | 1): string {
  const m = /^(\d{4})-W(\d{2})$/.exec(week)
  if (!m) return week

  const year = parseInt(m[1] ?? '0', 10)
  const weekNum = parseInt(m[2] ?? '0', 10)

  const jan4 = new Date(Date.UTC(year, 0, 4))
  const dayOfWeek = jan4.getUTCDay() || 7
  const firstMonday = new Date(jan4)
  firstMonday.setUTCDate(jan4.getUTCDate() - (dayOfWeek - 1))

  const targetMonday = new Date(firstMonday)
  targetMonday.setUTCDate(firstMonday.getUTCDate() + (weekNum - 1) * 7 + delta * 7)

  // Compute new year/week
  const newYear = targetMonday.getUTCFullYear()
  const newJan4 = new Date(Date.UTC(newYear, 0, 4))
  const newDow = newJan4.getUTCDay() || 7
  const newFirstMonday = new Date(newJan4)
  newFirstMonday.setUTCDate(newJan4.getUTCDate() - (newDow - 1))
  const newWeekNum =
    Math.round((targetMonday.getTime() - newFirstMonday.getTime()) / (7 * 86400 * 1000)) + 1

  return `${newYear}-W${String(newWeekNum).padStart(2, '0')}`
}

/**
 * Parse ISO week string 'YYYY-WNN' to Monday..Sunday date range (UTC).
 */
export function getWeekDateRange(week: string): { from: string; to: string } {
  const m = /^(\d{4})-W(\d{2})$/.exec(week)
  if (!m) throw new Error(`Invalid week format: ${week}`)

  const year = parseInt(m[1] ?? '0', 10)
  const weekNum = parseInt(m[2] ?? '0', 10)

  const jan4 = new Date(Date.UTC(year, 0, 4))
  const dayOfWeek = jan4.getUTCDay() || 7
  const monday = new Date(jan4)
  monday.setUTCDate(jan4.getUTCDate() - (dayOfWeek - 1) + (weekNum - 1) * 7)

  const sunday = new Date(monday)
  sunday.setUTCDate(monday.getUTCDate() + 6)
  sunday.setUTCHours(23, 59, 59, 999)

  return { from: monday.toISOString(), to: sunday.toISOString() }
}

/**
 * Convert total seconds to a human-readable "Xh Ym" format.
 */
export function secondsToHourMin(totalSeconds: number): string {
  const h = Math.floor(totalSeconds / 3600)
  const m = Math.floor((totalSeconds % 3600) / 60)
  return `${h}h ${String(m).padStart(2, '0')}m`
}
