"use client";

import { useTranslation } from "react-i18next";
import { holderMatchesOrganizationName, holderMatchesRegisteredName } from "../trainerRequest";

/**
 * The client's holder rule (answers PDF p.3, item 11) asks for a warning, not a
 * refusal: a holder that does not match the registered name goes to the
 * administration for manual review. Shown once the registered name is known
 * and the typed holder does not fit it; renders nothing otherwise.
 */
export default function HolderReviewNotice({
  holder,
  registeredName,
  kind,
}: {
  holder: string | null | undefined;
  registeredName: string | null | undefined;
  kind: "person" | "organization";
}) {
  const { t } = useTranslation();
  const typed = holder?.trim();
  const name = registeredName?.trim();
  if (!typed || !name) return null;
  const fits = kind === "organization"
    ? holderMatchesOrganizationName(typed, name)
    : holderMatchesRegisteredName(typed, name);
  if (fits) return null;
  return (
    <p role="status" className="mb-3 rounded-lg bg-amber-50 p-3 text-sm text-amber-900">
      {t("COMMON.BANK_HOLDER_REVIEW_NOTICE")}{" "}
      {t(kind === "organization" ? "COMMON.BANK_HOLDER_ORG_HINT" : "COMMON.BANK_HOLDER_MUST_MATCH_NAME")}
    </p>
  );
}

/**
 * The server's verdict on a saved holder (A11): under the admin's manual review
 * — no payout and no paid publishing until approved — or refused. Nothing for
 * `not_required` / `approved`, or an API that does not send it.
 */
export function HolderReviewStatus({ status, mismatch, className = "" }: { status?: string | null; mismatch?: boolean | null; className?: string }) {
  const { t } = useTranslation();
  if (status === "rejected") return <p role="status" className={`rounded-lg bg-red-50 p-3 text-sm text-red-700 ${className}`}>{t("COMMON.BANK_HOLDER_REVIEW_REJECTED")}</p>;
  if (status !== "pending" && mismatch !== true) return null;
  return <p role="status" className={`rounded-lg bg-amber-50 p-3 text-sm text-amber-900 ${className}`}>{t("COMMON.BANK_HOLDER_UNDER_REVIEW")}</p>;
}
