// ─────────────────────────────────────────────────────────────────────────────
// email-settings.mappers.ts
// API → UI mappers + small display-config maps (same role as inquiry.mappers.ts).
// ─────────────────────────────────────────────────────────────────────────────

import type {
  EmailSetting,
  EmailSettingRow,
} from "@/lib/api/types/email-settings.types";

// ─── Map raw API rows → table rows (adds a 1-based sno) ──────────────────────
export function mapEmailSettingsApiResponse(
  rows: EmailSetting[],
): EmailSettingRow[] {
  return (rows ?? []).map((r, i) => ({ ...r, sno: i + 1 }));
}

// ─── Encryption badge config ─────────────────────────────────────────────────
export const ENCRYPTION_CONFIG: Record<
  string,
  { label: string; bg: string; color: string }
> = {
  ssl: { label: "SSL", bg: "#E1F5EE", color: "#0F6E56" },
  tls: { label: "TLS", bg: "#E6F1FB", color: "#0C447C" },
  none: { label: "None", bg: "#FCEBEB", color: "#A32D2D" },
};

export function encryptionBadge(enc: string) {
  const key = (enc || "").toLowerCase();
  return (
    ENCRYPTION_CONFIG[key] ?? {
      label: enc || "—",
      bg: "#f1f5f9",
      color: "#6b7280",
    }
  );
}

// ─── Date formatting (matches formatDisplayDate in inquiry.mappers) ──────────
export function formatDisplayDate(d?: string | Date | null): string {
  if (!d) return "—";
  const dt = typeof d === "string" ? new Date(d) : d;
  if (isNaN(dt.getTime())) return "—";
  return dt.toLocaleDateString("en-GB", {
    day: "2-digit",
    month: "short",
    year: "numeric",
  });
}
