export const TIMED_MESSAGE_OPTIONS = [
  { label: "خاموش", value: null as number | null },
  { label: "۱۰ ثانیه", value: 10 },
  { label: "۳۰ ثانیه", value: 30 },
  { label: "۱ دقیقه", value: 60 },
  { label: "۵ دقیقه", value: 300 },
  { label: "۱ ساعت", value: 3600 },
] as const;

export function formatTimedLabel(seconds: number | null): string {
  const opt = TIMED_MESSAGE_OPTIONS.find((o) => o.value === seconds);
  return opt?.label ?? "خاموش";
}

export function getRemainingSeconds(expiresAt?: string | null): number | null {
  if (!expiresAt) return null;
  const ms = new Date(expiresAt).getTime() - Date.now();
  if (Number.isNaN(ms) || ms <= 0) return 0;
  return Math.ceil(ms / 1000);
}

export function formatCountdown(totalSeconds: number): string {
  if (totalSeconds >= 3600) {
    const h = Math.floor(totalSeconds / 3600);
    const m = Math.floor((totalSeconds % 3600) / 60);
    return `${h}:${String(m).padStart(2, "0")}`;
  }
  const m = Math.floor(totalSeconds / 60);
  const s = totalSeconds % 60;
  return `${m}:${String(s).padStart(2, "0")}`;
}

export function buildExpiresAtIso(seconds: number | null): string | undefined {
  if (!seconds) return undefined;
  return new Date(Date.now() + seconds * 1000).toISOString();
}
