export function formatRelativeTime(timestamp: number): string {
  const now = Date.now()
  const diffMs = now - timestamp
  const diffMin = Math.floor(diffMs / 60000)

  if (diffMin < 1) return 'now'
  if (diffMin < 60) return `${diffMin}m`
  const diffHr = Math.floor(diffMin / 60)
  if (diffHr < 24) return `${diffHr}h`
  const diffDay = Math.floor(diffHr / 24)
  if (diffDay < 7) return `${diffDay}d`
  const diffWeek = Math.floor(diffDay / 7)
  return `${diffWeek}w`
}

function toValidDate(timestamp: string | number): Date | null {
  const date = new Date(timestamp)
  return Number.isNaN(date.getTime()) ? null : date
}

function pad(value: number): string {
  return value.toString().padStart(2, '0')
}

export function formatMessageTimestamp(timestamp: string | number): string {
  const date = toValidDate(timestamp)
  if (!date) return ''

  const now = new Date()
  const isToday =
    date.getFullYear() === now.getFullYear() &&
    date.getMonth() === now.getMonth() &&
    date.getDate() === now.getDate()

  const timePart = `${pad(date.getHours())}:${pad(date.getMinutes())}`
  if (isToday) return timePart

  const monthDayPart = `${pad(date.getMonth() + 1)}-${pad(date.getDate())}`
  if (date.getFullYear() === now.getFullYear()) {
    return `${monthDayPart} ${timePart}`
  }

  return `${date.getFullYear()}-${monthDayPart} ${timePart}`
}

export function formatFullTimestamp(timestamp: string | number): string {
  const date = toValidDate(timestamp)
  if (!date) return ''

  const monthDayPart = `${pad(date.getMonth() + 1)}-${pad(date.getDate())}`
  const timePart = `${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`
  return `${date.getFullYear()}-${monthDayPart} ${timePart}`
}

export function formatCompletionDuration(ms: number): string {
  const seconds = Math.round(ms / 1000)
  if (seconds < 60) return `${seconds}s`
  const minutes = Math.round(seconds / 60)
  if (minutes < 60) return `${minutes}m`
  const hours = Math.round(minutes / 60)
  return `${hours}h`
}

export function formatElapsedTimer(ms: number): string {
  const totalSeconds = Math.floor(Math.max(0, ms) / 1000)
  const minutes = Math.floor(totalSeconds / 60)
  const seconds = totalSeconds % 60
  return `${minutes}:${seconds.toString().padStart(2, '0')}`
}

import { stripAnsi } from './ansi-utils'

export const COMPLETION_WORDS = [
  'Swarmed',
  'Buzzed',
  'Forged',
  'Brewed',
  'Waxed',
  'Honeyed',
  'Sealed',
  'Capped',
  'Foraged',
  'Scouted',
  'Sipped',
  'Clustered',
  'Nested',
  'Scented',
  'Pollinated',
  'Gathered',
  'Hummed'
]

const DEV_SERVER_URL_PATTERN = /https?:\/\/(?:localhost|127\.0\.0\.1|0\.0\.0\.0):\d{3,5}\/?/

export function extractDevServerUrl(output: string[]): string | null {
  // Scan last 50 entries for a dev server URL.
  // Each entry is a single line of output after line splitting.
  for (let i = output.length - 1; i >= Math.max(0, output.length - 50); i--) {
    const match = stripAnsi(output[i]).match(DEV_SERVER_URL_PATTERN)
    if (match) return match[0]
  }
  return null
}
