"use client";

import { useTranslation } from "react-i18next";
import { FaArrowTrendUp, FaClock, FaHandHoldingHeart, FaHandshake } from "react-icons/fa6";

import type { IndividualProfileData } from "./individualProfile";
import type { IconType } from "react-icons";
import type { CounterVisibility } from "@/features/profile/accountRecords";
import { wholeAchievementHours } from "@/features/achievements/leaderboardValue";
import { hasPositiveCount } from "@/lib/helpers";

/**
 * Literal class names — Tailwind only extracts whole strings.
 *
 * `visibilityKey` is the `counter_visibility` flag that governs each tile.
 * The server still sends hours and volunteering as always-true, so the
 * zero check below is what hides them until the person has any.
 */
const TILES: ReadonlyArray<{
  key: keyof IndividualProfileData["stats"];
  /* `keyof CounterVisibility`, so a tile cannot name a flag the backend does
     not send — the previous `Record<string, boolean>` on the other side made
     these three strings unchecked. */
  visibilityKey: keyof CounterVisibility;
  Icon: IconType;
  labelKey: string;
  className: string;
}> = [
  { key: "hours", visibilityKey: "volunteer_hours", Icon: FaClock, labelKey: "COMMON.PROFILE_STAT_HOURS", className: "border-primary-501 text-primary-501" },
  { key: "volunteerOpportunities", visibilityKey: "volunteer_opportunities", Icon: FaHandHoldingHeart, labelKey: "COMMON.PROFILE_STAT_VOLUNTEER", className: "border-primary-502 text-primary-502" },
  { key: "developmentOpportunities", visibilityKey: "development", Icon: FaArrowTrendUp, labelKey: "COMMON.PROFILE_STAT_DEVELOPMENT", className: "border-primary-503 text-primary-503" },
  /* Organizations only — the other kinds carry no `sponsorships`, so the zero
     rule drops it. */
  { key: "sponsorships", visibilityKey: "sponsorship", Icon: FaHandshake, labelKey: "COMMON.PROFILE_STAT_SPONSORSHIPS", className: "border-primary-801 text-primary-801" },
];

/** Literal, because the column count changes with how many tiles survive. */
const GRID_COLS = ["grid-cols-1", "grid-cols-1", "grid-cols-2", "grid-cols-3", "grid-cols-2"];

export default function ProfileStatTiles({
  stats,
  counterVisibility,
}: {
  stats: IndividualProfileData["stats"];
  counterVisibility?: IndividualProfileData["counterVisibility"];
}) {
  const { t } = useTranslation();

  // A tile shows only once its counter is 1 or more (the client's rule), and
  // never when the backend's `counter_visibility` turns it off. A payload with
  // no visibility block predates the field, so only the zero rule applies.
  const tiles = TILES.filter(
    ({ key, visibilityKey }) =>
      hasPositiveCount(stats[key]) &&
      (!counterVisibility || counterVisibility[visibilityKey] !== false)
  );

  if (tiles.length === 0) return null;

  return (
    <section className={`grid gap-3 ${GRID_COLS[tiles.length]}`}>
      {tiles.map(({ key, Icon, labelKey, className }) => (
        <div
          key={key}
          className={`flex flex-col items-center rounded-2xl border-2 bg-white px-2 py-4 text-center ${className}`}
        >
          <Icon size={24} aria-hidden />
          <span className="mt-2 text-2xl font-bold">{key === "hours" ? wholeAchievementHours(stats[key]) : stats[key]}</span>
          <span className="mt-1 text-xs font-semibold">{t(labelKey)}</span>
        </div>
      ))}
    </section>
  );
}
