"use client";

import Link from "next/link";

/**
 * The Business-mode header pieces (PRD §4.1, LEIS-1014).
 *
 * Split out of `SiteHeader` because each renders twice — once in the compact
 * header below 1226px and once in the full one above it — and the two copies
 * drifting is exactly the bug this file prevents.
 *
 * A `ModeToggle` lived here too, linking out to Personal mode on
 * leisure-ui.com. Removed on request: corp is Business-only, and a control
 * whose single purpose is to leave is not chrome this product needs.
 */

/**
 * "leisure.com · myBiz".
 *
 * The product name sits beside the existing logo rather than replacing it:
 * there is no myBiz asset in the repo, and inventing one is a brand decision,
 * not an implementation detail. A text pill is honest, scales, and needs no
 * second image request.
 */
export function MyBizBadge() {
  return (
    <span
      className="inline-flex items-center rounded-[6px] px-[8px] py-[3px] text-[12px] leading-none font-bold tracking-[0.01em] text-white"
      style={{ backgroundColor: "var(--fra-navy)" }}
    >
      myBiz
    </span>
  );
}

/**
 * The avatar, as initials rather than a generic silhouette.
 *
 * Both home pages greet the user by name, and an admin booking for somebody
 * else needs to be able to tell at a glance whose session this is — a shared
 * machine in a travel desk is the normal case here, not an edge one.
 */
export function InitialsAvatar({ name, email }: { name: string | null; email: string | null }) {
  const source = (name || email || "").trim();
  const initials =
    source
      .replace(/@.*$/, "")
      .split(/[\s._-]+/)
      .filter(Boolean)
      .slice(0, 2)
      .map((part) => part[0]?.toUpperCase() ?? "")
      .join("") || "?";

  return (
    <span
      className="inline-flex size-[36px] items-center justify-center rounded-[10px] text-[13px] font-bold"
      style={{ backgroundColor: "var(--fra-blue-bg)", color: "var(--fra-navy)" }}
      aria-hidden="true"
    >
      {initials}
    </span>
  );
}

/**
 * The one role-specific link in the header.
 *
 * An employee's is My requests; the admin's is the approval queue, with the
 * number waiting on them. The count is the reason an admin opens this app at
 * all, so it belongs in the chrome rather than one page deep.
 */
export function RoleLink({
  href,
  label,
  icon,
  count,
  onClick,
}: {
  href: string;
  label: string;
  icon: React.ReactNode;
  /** Pending approvals. Omitted or 0 renders no badge — "nothing waiting" is
   *  not a number worth drawing. */
  count?: number;
  onClick?: () => void;
}) {
  return (
    <Link
      href={href}
      onClick={onClick}
      className="inline-flex min-h-[44px] items-center gap-[8px] rounded-[10px] px-[10px] text-[15px] font-medium no-underline"
      style={{ color: "var(--fra-text)" }}
    >
      <span className="flex items-center" style={{ color: "var(--fra-muted)" }}>
        {icon}
      </span>
      {label}
      {typeof count === "number" && count > 0 && (
        <span
          className="inline-flex min-w-[20px] items-center justify-center rounded-[6px] px-[5px] py-[2px] text-[11px] leading-none font-bold text-white"
          style={{ backgroundColor: "var(--fra-coral)" }}
        >
          {count > 99 ? "99+" : count}
          <span className="sr-only"> waiting for approval</span>
        </span>
      )}
    </Link>
  );
}
