import Link from "next/link";

/**
 * One tile in a `.bz-stats` row (LEIS-1021).
 *
 * Lifted out of `components/travel-policy/TravelPolicyPage.tsx`, where it was a
 * module-private `Stat`, because "Company at a glance" on the admin home needs
 * the same four-up row of figures. Two copies of a tile whose CSS lives in a
 * vendored sheet is how one of them silently stops matching the other.
 *
 * ── Why `.bz-*` and not `.fra-stats` ────────────────────────────────────
 *
 * The approval queue's stat row is `.fra-stats`, whose grid is hard-wired to
 * `repeat(3, 1fr) 1.9fr` — three tiles and a wide spend panel. "Company at a
 * glance" is four equal cards, which that grid cannot express. `.bz-stats` is
 * `repeat(4, 1fr)`, responsive, and `biz-ui.css` is loaded from the ROOT
 * layout, so it works on any route rather than only under `/admin`.
 *
 * `value` is a string, not a number as the original took: three of the four
 * cards on D0 are money, and money arrives from the API as a decimal string
 * that must not go near a float.
 */
export function BzStat({
  tone = "navy",
  icon,
  label,
  value,
  sub,
  href,
}: {
  tone?: "navy" | "coral" | "green" | "amber" | "indigo";
  /** A Bootstrap Icons class, e.g. `bi-hourglass-split`. */
  icon: string;
  label: string;
  value: string;
  sub?: string;
  /** Makes the whole tile a link — the ticket's "each card opens the queue
   *  with the matching filter". Omit and it renders as a plain figure. */
  href?: string;
}) {
  const body = (
    <>
      <span className="bz-stat-icon" aria-hidden="true">
        <i className={`bi ${icon}`} />
      </span>
      <div>
        <small>{label}</small>
        <strong>{value}</strong>
        {/* Always rendered, even when empty, so a row of tiles keeps one
            baseline whether or not each has a sub-line. */}
        <em>{sub ?? " "}</em>
      </div>
    </>
  );

  if (!href) {
    return <div className={`bz-stat tone-${tone}`}>{body}</div>;
  }

  return (
    <Link href={href} className={`bz-stat tone-${tone}`} style={{ textDecoration: "none" }}>
      {body}
    </Link>
  );
}
