import type { User } from "@/store/authStore";

/** The fields the switcher reads off a stored session. */
export type AccountSummary = Pick<
  User,
  | "id"
  | "first_name"
  | "last_name"
  | "company_name"
  | "nickname"
  | "email"
  | "user_type"
  /* Optional on `User`, and absent until something fills it in — the sign-in
     payload does not carry it. The row falls back to the organization mark
     when it is missing, which is also the right mark for an organization. */
  | "gender_display"
>;

/** Prefer profile names; email is only a fallback for unnamed sessions. */
export function accountLabel(
  account: Pick<User, "id" | "first_name" | "last_name" | "email" | "company_name" | "nickname">
): string {
  const companyName = (account.company_name ?? "").trim();
  if (companyName) return companyName;
  const name = [account.first_name, account.last_name]
    .map((part) => (part ?? "").trim())
    .filter(Boolean)
    .join(" ");
  if (name) return name;
  const nickname = (account.nickname ?? "").trim();
  if (nickname) return nickname;
  const email = (account.email ?? "").trim();
  if (email) return email;
  return `#${account.id}`;
}

/**
 * The i18n key for "volunteer" or "entity", shown under the name.
 *
 * Two accounts can share a name and differ only in kind — the same person
 * signed in as themselves and as the organization they run is the ordinary
 * case here, not an edge one — so the row says which is which.
 *
 * `individual` is the frontend-only alias the guards collapse to `volunteer`;
 * it is folded the same way so the label cannot disagree with the guard.
 * `admin` has no screen in this app, so it gets no label rather than a wrong
 * one.
 */
export function accountKindKey(
  account: Pick<User, "user_type">
): string | null {
  switch (account.user_type) {
    case "organization":
      return "COMMON.ACCOUNT_TYPE_ORGANIZATION";
    case "volunteer":
    case "individual":
      return "COMMON.ACCOUNT_TYPE_VOLUNTEER";
    default:
      return null;
  }
}
