"use client";

import type { IconType } from "react-icons";
import { useTranslation } from "react-i18next";
import {
  FaBriefcase,
  FaChild,
  FaGraduationCap,
  FaHandHoldingHeart,
  FaHeartPulse,
  FaLeaf,
  FaMicrochip,
  FaPalette,
  FaPaw,
  FaPenRuler,
  FaStar,
} from "react-icons/fa6";

import { useLanguageStore } from "@/store/languageStore";
import type { LocalizedChoice } from "./individualProfile";

/**
 * Interests are admin-editable choices with no icon field, so the icon is
 * matched on the English name; anything new falls back to a star rather than
 * showing nothing.
 */
const ICON_RULES: Array<[RegExp, IconType]> = [
  [/kid|child/i, FaChild],
  [/art|culture/i, FaPalette],
  [/health|sport/i, FaHeartPulse],
  [/educat|learn|train/i, FaGraduationCap],
  [/business|econom/i, FaBriefcase],
  [/design/i, FaPenRuler],
  [/environment|sustain/i, FaLeaf],
  [/tech|innovat/i, FaMicrochip],
  [/animal/i, FaPaw],
  [/volunteer|humanit|relief|community/i, FaHandHoldingHeart],
];

const iconFor = (valueEn?: string | null): IconType =>
  ICON_RULES.find(([pattern]) => pattern.test(valueEn ?? ""))?.[1] ?? FaStar;

export default function ProfileInterestChips({ interests }: { interests: LocalizedChoice[] }) {
  const { t } = useTranslation();
  const language = useLanguageStore((s) => s.language);
  if (interests.length === 0) return null;

  return (
    <section className="rounded-3xl border border-[#29246D14] bg-white p-5">
      <h2 className="mb-3 flex items-center gap-2 font-bold text-primary-5">
        <span className="h-2 w-2 rounded-full bg-primary-5" aria-hidden />
        {t("COMMON.INTEREST")}
      </h2>
      <div className="flex flex-wrap gap-2">
        {interests.map((interest, index) => {
          const Icon = iconFor(interest.value_en);
          return (
            <span
              // The website payloads may omit `id` (see `LocalizedChoice`).
              key={interest.id ?? interest.value_en ?? index}
              className="inline-flex items-center gap-2 rounded-full border border-[#29246D26] px-3 py-1 text-sm text-primary-5"
            >
              <Icon size={13} aria-hidden />
              {language === "ar" ? interest.value_ar : interest.value_en}
            </span>
          );
        })}
      </div>
    </section>
  );
}
