/**
 * Utilidades de huso horario para Alpha.
 *
 * Colombia: UTC-5 (sin horario de verano).
 * Microsoft Teams transcripts incluyen UTC en el filename.
 */

const COLOMBIA_OFFSET = -5 // UTC-5

/**
 * Parsea timestamp UTC desde filename de Microsoft Teams.
 * Formato: "305 Equilibrium Logicas Dashboard Informes-20260525_185916UTC-Meeting Recording"
 *                                                         ^^^^^^^^^^^^^^^^^^^^
 * Devuelve: { dateStr: "2026-05-25", timeStr: "13:59", utc: "18:59" }
 */
export function parseTeamsUTC(filename: string): { dateStr: string; timeStr: string; utc: string } | null {
  // Busca patrón YYYYMMDD_HHMMSSUTC
  const match = filename.match(/(\d{4})(\d{2})(\d{2})_(\d{2})(\d{2})(\d{2})UTC/)
  if (!match) return null

  const [, y, m, d, hh, mm] = match

  // Crear fecha UTC
  const utcDate = new Date(Date.UTC(Number(y), Number(m) - 1, Number(d), Number(hh), Number(mm)))

  // Convertir a Colombia (UTC-5)
  const colombia = new Date(utcDate.getTime() + COLOMBIA_OFFSET * 3600000)

  const pad2 = (n: number) => String(n).padStart(2, '0')

  const dateStr = `${colombia.getFullYear()}-${pad2(colombia.getMonth() + 1)}-${pad2(colombia.getDate())}`
  const timeStr = `${pad2(colombia.getHours())}:${pad2(colombia.getMinutes())}`
  const utcStr = `${hh}:${mm}`

  return { dateStr, timeStr, utc: utcStr }
}

/**
 * Detecta si el filename es de Microsoft Teams.
 */
export function isTeamsFile(filename: string): boolean {
  return /Meetings?\s*(Recording|Transcript)/i.test(filename) || /^\d{8}_\d{6}UTC/.test(filename)
}

/**
 * Convierte una fecha UTC a string en hora Colombia.
 */
export function toColombiaTime(isoString: string): string {
  const d = new Date(isoString)
  const col = new Date(d.getTime() + COLOMBIA_OFFSET * 3600000)
  const pad2 = (n: number) => String(n).padStart(2, '0')
  return `${col.getFullYear()}-${pad2(col.getMonth() + 1)}-${pad2(col.getDate())} ${pad2(col.getHours())}:${pad2(col.getMinutes())}`
}
