/**
 * Friends, or followers.
 * ----------------------------------------------------------------------
 * A site runs one of two social models, chosen by the admin:
 *
 *   "follow"  — you follow people; they follow you back or they don't.
 *   "friend"  — you ask, they accept, and the connection is mutual.
 *
 * Both run on the SAME `follows` rows. Nothing is migrated when the mode
 * changes, and changing it back leaves the data exactly as it was. That
 * is the whole point: the two models are different readings of one table,
 * not two tables.
 *
 *   a follow  a -> b            "a follows b"        /  "a asked b"
 *   both ways a -> b and b -> a "they follow each other" / "they are friends"
 *
 * The request list is derived either way, which is why this works without
 * a new collection: someone pointing at you that you don't point back at
 * is a follower in one mode and an unanswered request in the other. The
 * app already read it that way (app/api/follow-requests) before friends
 * existed; friend mode only changes what it is called and what accepting
 * does.
 */

export type ConnectionMode = "follow" | "friend";

/** What the admin chose, defaulting to how the site has always behaved. */
export function connectionMode(settings: Record<string, unknown> | null | undefined): ConnectionMode {
  return settings?.connectionMode === "friend" ? "friend" : "follow";
}

/**
 * Every piece of wording that changes with the mode, in one place.
 *
 * Scattering `mode === "friend" ? "Friends" : "Followers"` through the
 * components is how half a site ends up saying "follow" after the admin
 * switched to friends. Components ask this object instead.
 */
export type ConnectionWords = {
  mode: ConnectionMode;
  /** The connection itself: "Friends" / "Followers". */
  plural: string;
  /** One of them: "Friend" / "Follower". */
  singular: string;
  /** The button before you're connected. */
  connect: string;
  /** The button once you are. */
  connected: string;
  /** The button that ends it, shown on hover/again. */
  disconnect: string;
  /** The button after you've asked but they haven't answered. */
  pending: string;
  /** The button when THEY asked first, so tapping answers rather than asks. */
  accept: string;
  /** The heading on the requests card. */
  requests: string;
  /** What the notification says when someone connects to you. */
  notifyText: string;
  /** The tab that lists people you point at. Empty when it isn't shown. */
  outgoing: string;
  /** Whether the profile shows followers/following alongside friends. */
  showFollowCounts: boolean;
};

const FOLLOW: ConnectionWords = {
  mode: "follow",
  plural: "Followers",
  singular: "Follower",
  connect: "Follow",
  connected: "Following",
  disconnect: "Unfollow",
  // Following someone takes effect at once, so nothing is ever pending.
  pending: "Following",
  // Nothing to answer in follow mode -- following back is just following.
  accept: "Follow back",
  requests: "Follow requests",
  notifyText: "started following you",
  outgoing: "Following",
  showFollowCounts: true,
};

const FRIEND: ConnectionWords = {
  mode: "friend",
  plural: "Friends",
  singular: "Friend",
  connect: "Add friend",
  connected: "Friends",
  disconnect: "Unfriend",
  pending: "Requested",
  accept: "Confirm",
  requests: "Friend requests",
  notifyText: "sent you a friend request",
  // In friend mode there is no separate audience to list: a connection is
  // mutual or it is a request, and both are already shown.
  outgoing: "",
  showFollowCounts: false,
};

export function words(mode: ConnectionMode): ConnectionWords {
  return mode === "friend" ? FRIEND : FOLLOW;
}
