"use client";
import type { ReactNode } from "react";
import { useAuthStore } from "@/store/authStore";

import { useTranslation } from "react-i18next";
import { Mail, Pencil, RotateCcw, Trash2 } from "lucide-react";
import ManageActionIcon from "@/components/ui/ManageActionIcon";
import { Button } from "@/components/ui/Button";

interface LearnServeActionRailProps {
  /** The mobile call renders the viewer's button alone — see the note below. */
  mobile: boolean;
  isCreator: boolean;
  isRepostState: boolean;
  showActionButton: boolean;
  closedByCreator: boolean;
  canRequestDeletion: boolean;
  actionButtonLabel: string;
  /** A paid booking — «تم الحجز» is a state, not a button. */
  actionDisabled?: boolean;
  editDisabled?: boolean;
  deleteDisabled?: boolean;
  onRegisterClick: () => void;
  onPrimaryAction: () => void;
  onDeleteOpportunity: () => void;
  /** Opens the compose dialog for messaging everyone registered. */
  onMessageRegistrants?: () => void;
  messagePending?: boolean;
  cancellationAction?: ReactNode;
}

/**
 * Matches `VolunteerEvent`: the creator's controls are the shared icon row,
 * a viewer keeps the labelled Register / Unregister button.
 *
 * The icon row renders from the desktop call only (`!mobile`) but carries no
 * `xss:hidden`, so it is visible at every width — unlike the buttons it
 * replaced, whose mobile counterpart never existed. The `mobile` call is left
 * with the viewer's button alone, or the row would appear twice on a phone.
 */
export default function LearnServeActionRail({
  mobile,
  isCreator,
  isRepostState,
  showActionButton,
  closedByCreator,
  canRequestDeletion,
  actionButtonLabel,
  actionDisabled = false,
  editDisabled = false,
  deleteDisabled = false,
  onRegisterClick,
  onPrimaryAction,
  onDeleteOpportunity,
  onMessageRegistrants,
  messagePending = false,
  cancellationAction,
}: LearnServeActionRailProps) {
  const { t } = useTranslation();
  const publishingPaused = useAuthStore(state => state.user?.is_publishing_paused === true);

  return (
    <div
      className={
        mobile ? "flex flex-col" : "flex flex-col items-end gap-3 pt-1"
      }
    >
      <div className={mobile ? "flex flex-col items-center gap-3 my-6" : "flex flex-col items-end gap-3 xss:hidden"}>
      {showActionButton && !isCreator && (
        <Button
          variant="primary"
          size="medium"
          className={
            mobile
              ? "whitespace-nowrap"
              : "whitespace-nowrap block xss:hidden"
          }
          onClick={onRegisterClick}
          disabled={actionDisabled}
        >
          {actionButtonLabel}
        </Button>
      )}

      {!isCreator && cancellationAction}
      </div>

      {!isCreator && actionButtonLabel === t("COMMON.REGISTERED") && <p className="max-w-xs text-center text-xs leading-relaxed text-secondary-102">{t("COMMON.REGISTRATION_CANCELLATION_CUTOFF")}</p>}

      {/* Only when no state button is shown (an organization viewer); otherwise the label would repeat */}
      {closedByCreator && !isCreator && !showActionButton && (
        <span className="whitespace-nowrap rounded-[20px] bg-[#F1F1F5] px-4 py-2 text-sm font-bold text-secondary-102">
          {t("COMMON.REGISTRATION_UNAVAILABLE")}
        </span>
      )}

      {!mobile && isCreator && (
        <div className="flex flex-wrap  flex-col items-center justify-end gap-2">
          {showActionButton && (
            <ManageActionIcon
              label={actionButtonLabel}
              icon={
                isRepostState ? (
                  <RotateCcw className="h-5 w-5" />
                ) : (
                  <Pencil className="h-5 w-5" />
                )
              }
              onClick={onPrimaryAction}
              disabled={isRepostState ? publishingPaused : editDisabled}
            />
          )}

          {/* Development has no organizer-controlled close/reopen action. */}
          {/* Message everyone registered — the same icon and dialog as the
              volunteering rail (`CreatorActionBar`). The API 403s a
              non-creator either way. */}
          {onMessageRegistrants && (
            <ManageActionIcon
              label={t("COMMON.MESSAGE_REGISTRANTS")}
              icon={<Mail className="h-5 w-5" />}
              onClick={onMessageRegistrants}
              disabled={messagePending}
            />
          )}

          {canRequestDeletion && (
            <ManageActionIcon
              label={t("COMMON.DELETE_OPPORTUNITY")}
              icon={<Trash2 className="h-5 w-5" />}
              onClick={onDeleteOpportunity}
              disabled={deleteDisabled}
              danger
            />
          )}
        </div>
      )}
    </div>
  );
}
