"use client";

import { useTranslation } from "react-i18next";
import { Download } from "lucide-react";
import { Button } from "@/components/ui/Button";
import Searchbar from "@/components/ui/Searchbar";
import CheckInWindowBanner from "./CheckInWindowBanner";
import CertificatesStateError from "./CertificatesStateError";
import { SEND_LEARN_SERVE_CERTIFICATES_LIVE } from "@/features/opportunities/learnServeCertificates";
import type { getCheckInWindow } from "@/features/opportunities/checkInWindow";

interface RegisterListToolbarProps {
  onSearchChange: (value: string) => void;
  isSearching?: boolean;
  checkInWindow: ReturnType<typeof getCheckInWindow>;
  /** False for a learning type that grants no certificate at all. */
  canGrantCertificates: boolean;
  /** A key when it is too early to send; falsy when sending is allowed. */
  sendBlockedReason: string | null | undefined;
  isSendingCertificates: boolean;
  /** A15 — the server's «تم تعديل الساعات…» note while a certificate is out of date. */
  certificatesNote?: string | null;
  /** The failed A15 read behind a `load-failed` reason, and its retry. */
  certificatesError?: unknown;
  onRetryCertificates?: () => void;
  onSendCertificates: () => void;
  onDownloadSheet: () => void;
  hasRows: boolean;
  /** BE-125 — only a paid opportunity has money to show. */
}

/** Search, the check-in deadline banner, and the two export-side actions. */
export default function RegisterListToolbar({
  onSearchChange,
  isSearching = false,
  checkInWindow,
  canGrantCertificates,
  sendBlockedReason,
  isSendingCertificates,
  certificatesNote,
  certificatesError,
  onRetryCertificates,
  onSendCertificates,
  onDownloadSheet,
  hasRows,
}: RegisterListToolbarProps) {
  const { t } = useTranslation();

  return (
    <>
      <div className="flex justify-center lg:pb-[35px] md:pb-[20px] pb-[20px] searchbox">
        <Searchbar
          placeholder={t("COMMON.SEARCH_BY_NAME")}
          showFilterIcon={false}
          onSearchChange={onSearchChange}
          isLoading={isSearching}
        />
      </div>

      <p className="mx-auto max-w-3xl mb-4 text-center text-sm leading-relaxed text-secondary-102">
        {t("COMMON.CONFIRM_ATTENDANCE")}
      </p>
      <CheckInWindowBanner
        window={checkInWindow}
        className="mb-6 max-w-2xl mx-auto text-center"
      />

      {/* Export sits above the table, where the organizer is looking when
          they want it — it used to be at the very bottom of the page. */}
      {/* `justify-end` rather than a hardcoded side: the mockup puts it at
          the far left, which in this RTL page is the end of the row — and
          stays the correct corner when the page is switched to English. */}
      <div className="mb-6 flex flex-wrap items-center justify-end gap-3">
        {/* BE-102 — hidden entirely for a type that grants no certificate,
            so the organizer is never offered something that would 422. */}
        {SEND_LEARN_SERVE_CERTIFICATES_LIVE && canGrantCertificates && (
          <Button
            variant="primary"
            size="medium"
            onClick={onSendCertificates}
            disabled={Boolean(sendBlockedReason) || isSendingCertificates}
            title={
              sendBlockedReason === "no-attendee"
                ? t("COMMON.CERTIFICATES_NEED_ATTENDEE")
                : sendBlockedReason === "up-to-date"
                  ? t("COMMON.CERTIFICATES_UP_TO_DATE")
                  : sendBlockedReason === "load-failed"
                  ? t("COMMON.CERTIFICATES_STATE_LOAD_FAILED")
                  : sendBlockedReason === "loading"
                  ? undefined
                  : sendBlockedReason
                  ? t("COMMON.CERTIFICATES_AFTER_END")
                  : undefined
            }
          >
            {t("COMMON.SEND_CERTIFICATES")}
          </Button>
        )}
        {SEND_LEARN_SERVE_CERTIFICATES_LIVE && canGrantCertificates && sendBlockedReason === "load-failed" && onRetryCertificates && (
          <CertificatesStateError error={certificatesError} onRetry={onRetryCertificates} />
        )}
        {SEND_LEARN_SERVE_CERTIFICATES_LIVE && canGrantCertificates && certificatesNote && (
          <p role="status" className="basis-full text-end text-sm text-secondary-102">{certificatesNote}</p>
        )}
        {/* BE-125 — the money table, which only exists for a paid
            opportunity: a free one has no transaction behind any row, so the
            screen would be a grid of dashes. */}
        <Button
          variant="primary"
          size="medium"
          className="gap-2"
          onClick={onDownloadSheet}
          disabled={!hasRows}
        >
          <Download className="h-5 w-5" />
          {t("COMMON.SHEET")}
        </Button>
      </div>
    </>
  );
}
