import type { ViharStatusT } from '@vihar/shared';

export function fmtDate(iso: string): string {
  return new Date(iso).toLocaleDateString('en-IN', {
    day: 'numeric', month: 'short', weekday: 'short', timeZone: 'UTC',
  });
}

export function fmtDateLong(iso: string): string {
  return new Date(iso).toLocaleDateString('en-IN', {
    day: 'numeric', month: 'long', weekday: 'long', year: 'numeric', timeZone: 'UTC',
  });
}

export function fmtTime(hhmmss: string): string {
  const [h, m] = hhmmss.split(':').map(Number);
  const period = (h ?? 0) >= 12 ? 'PM' : 'AM';
  const h12 = (h ?? 0) % 12 === 0 ? 12 : (h ?? 0) % 12;
  return `${h12}:${String(m ?? 0).padStart(2, '0')} ${period}`;
}

export function fmtTimestamp(iso: string): string {
  return new Date(iso).toLocaleTimeString('en-IN', { hour: 'numeric', minute: '2-digit', hour12: true });
}

export function initials(name: string): string {
  return name.split(' ').map((w) => w[0]).join('').slice(0, 2).toUpperCase();
}

const STATUS_LABELS: Record<ViharStatusT, string> = {
  planned: 'Planned',
  in_progress: 'In Progress',
  completed: 'Completed',
  cancelled: 'Cancelled',
  auto_closed: 'Auto-Closed',
};

export function statusLabel(s: ViharStatusT): string {
  return STATUS_LABELS[s];
}

export function statusPillClass(s: ViharStatusT): string {
  return `status-pill status-pill--${s.replace('_', '-')}`;
}
