"use client";

import { useTranslation } from "react-i18next";
import Button from "@/components/ui/Button";
import { getApiErrorMessage } from "@/lib/api/errors";
import { useLanguageStore } from "@/store/languageStore";

/**
 * A community read that failed (PDF p.37: a clear error state, never an empty
 * list that reads as «no posts»). The server's own message when it sent one —
 * a 404 arrives localized — otherwise `fallbackKey`; retry when it can help.
 */
export default function CommunityLoadError({
  error,
  fallbackKey,
  onRetry,
}: {
  error: unknown;
  fallbackKey: string;
  onRetry?: () => void;
}) {
  const { t } = useTranslation();
  const language = useLanguageStore((s) => s.language);
  return (
    <div role="alert" className="mx-auto max-w-3xl px-4 py-16 text-center">
      <p className="mb-6 text-lg text-red-600">
        {getApiErrorMessage(error, language) || t(fallbackKey)}
      </p>
      {onRetry && (
        <Button size="medium" onClick={onRetry}>
          {t("COMMON.RETRY")}
        </Button>
      )}
    </div>
  );
}
