"use client";
import { formatHoursMinutes } from "@/features/shared/clientReviewPolicy";

import AchievementCard from "./AchievementCard";
import { asset } from "./commonProfileTypes";
import { hasPositiveCount, toDisplayDigits } from "@/lib/helpers";

/**
 * The achievement tiles.
 *
 * The grid drops to two columns at `xl` when there are four cards, so the row
 * never leaves a single tile stranded on a second line.
 */
export default function ProfileStatsRow({
  cards,
}: {
  cards: Array<{
    icon: string;
    value: number | string | null | undefined;
    label: string;
    color: string;
  }>;
}) {
  // A counter still at zero is not shown (the client's rule).
  const shown = cards.filter((card) => hasPositiveCount(card.value));
  if (shown.length === 0) return null;

  return (
          <div
            className={`grid gap-[30px] mobilescreen:gap-3 grid-flow-col auto-cols-[minmax(120px,1fr)] overflow-x-auto pb-2 md:grid-flow-row md:auto-cols-auto md:overflow-visible md:pb-0 md:grid-cols-3 ${
              shown.length === 4 ? "xl:grid-cols-2" : "xl:grid-cols-3"
            }`}
          >
            {shown.map((card) => (
              <AchievementCard
                key={card.icon}
                icon={asset(card.icon)}
                // Digits stay Western in both languages; the axios layer has
                // already normalized the API's Arabic-Indic strings, and this
                // also pins a missing counter to "0".
                value={card.icon.toLowerCase().includes("hours") ? formatHoursMinutes(card.value) : toDisplayDigits(card.value)}
                label={card.label}
                colorClass={card.color}
              />
            ))}
          </div>
  );
}
