import { AVATAR_FALLBACK_CLASS } from "./athlete-avatar";
import { PersonFillIcon } from "./icons";

/** Smallest allowed hit target in px (touch guideline). */
const MIN_HIT_PX = 44;

/** Props for {@link AvatarButton}. */
export interface AvatarButtonProps {
  /** Profile photo URL; the profile icon is shown when absent. */
  photoUrl?: string | null;
  /** Click handler, used when `href` is not set. */
  onPress?: () => void;
  /** When set, an `<a>` is rendered instead of a `<button>`. */
  href?: string;
  /** Visual diameter in px (default 44). The hit area never drops below 44 px. */
  size?: number;
  /** Draws a 1.5px `--foreground-red-orange` ring behind a 2px page-background gap. */
  ring?: boolean;
  /** Accessible name; the caller passes the translated copy, e.g. `t("Account")`. */
  ariaLabel: string;
  /** Extra classes on the outer hit-area element. */
  className?: string;
  /** Optional `data-find` attribute for test/find hooks. */
  dataFind?: string;
}

const RING_SHADOW =
  "0 0 0 2px var(--background), 0 0 0 3.5px var(--foreground-red-orange)";

/**
 * Round avatar that opens the Account page: a photo, or the profile icon on
 * a solid primary fill. Renders an `<a>` with `href`, otherwise a button.
 *
 * @remarks
 * The outer element is always at least 44 x 44 px; a smaller `size` is centred
 * inside it so the visual circle shrinks but the tap area does not.
 */
export function AvatarButton({
  photoUrl,
  onPress,
  href,
  size = MIN_HIT_PX,
  ring = false,
  ariaLabel,
  className,
  dataFind,
}: AvatarButtonProps) {
  const hit = Math.max(size, MIN_HIT_PX);
  const classes = `inline-flex shrink-0 items-center justify-center rounded-full focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-foreground-red-orange ${className ?? ""}`;
  const style = { width: hit, height: hit, minWidth: MIN_HIT_PX, minHeight: MIN_HIT_PX };
  const circle = <AvatarCircle photoUrl={photoUrl} size={size} ring={ring} />;
  if (href) {
    return (
      <a href={href} aria-label={ariaLabel} className={classes} style={style} data-find={dataFind}>
        {circle}
      </a>
    );
  }
  return (
    <button type="button" onClick={onPress} aria-label={ariaLabel} className={classes} style={style} data-find={dataFind}>
      {circle}
    </button>
  );
}

/**
 * The avatar circle of {@link AvatarButton} on its own, without a hit area or
 * a role: a photo, or the profile icon on the fallback fill. Never initials:
 * it stands for the signed-in account, and the icon reads as "your profile"
 * where two letters read as someone's name tag. For places where the avatar is
 * decoration next to a control that already carries the action.
 */
export function AvatarCircle({
  photoUrl,
  size = MIN_HIT_PX,
  ring = false,
}: {
  /** Profile photo URL; the profile icon is shown when absent. */
  photoUrl?: string | null;
  /** Diameter in px (default 44). */
  size?: number;
  /** See {@link AvatarButtonProps.ring}. */
  ring?: boolean;
}) {
  return (
    <span
      className={`flex shrink-0 items-center justify-center overflow-hidden rounded-full ${AVATAR_FALLBACK_CLASS}`}
      style={{
        width: size,
        height: size,
        boxShadow: ring ? RING_SHADOW : undefined,
      }}
      data-ring={ring ? "true" : undefined}
    >
      {photoUrl ? (
        // eslint-disable-next-line @next/next/no-img-element
        <img src={photoUrl} alt="" className="size-full object-cover" />
      ) : (
        <PersonFillIcon className="size-1/2" />
      )}
    </span>
  );
}
