import type { FleetResourceConfig } from "@/pages/fleet/config/resources";

export interface FleetCardPresentation {
  titleKey: string;
  subtitleKey?: string;
  codeKey?: string;
  statusKey?: string;
}

export const resolveFleetCardPresentation = (config: FleetResourceConfig): FleetCardPresentation => {
  const titleKey =
    config.cardTitleKey ??
    config.columns.find((col) => col.format !== "code" && col.accessorKey !== "status")
      ?.accessorKey ??
    "id";

  const codeKey =
    config.cardCodeKey ?? config.columns.find((col) => col.format === "code")?.accessorKey;

  const statusKey = config.columns.find((col) => col.format === "statusBadge")?.accessorKey;

  const subtitleKey =
    config.cardSubtitleKey ??
    config.columns.find(
      (col) =>
        col.accessorKey !== titleKey &&
        col.accessorKey !== codeKey &&
        col.accessorKey !== statusKey,
    )?.accessorKey;

  return { titleKey, subtitleKey, codeKey, statusKey };
};

export const cardInitials = (title: string) => {
  const parts = title.trim().split(/\s+/).filter(Boolean);
  if (!parts.length) return "?";
  if (parts.length === 1) return parts[0].slice(0, 2).toUpperCase();
  return `${parts[0][0] ?? ""}${parts[1][0] ?? ""}`.toUpperCase();
};
