"use client";

import { Lock, Pencil, RotateCcw } from "lucide-react";
import { useTranslation } from "react-i18next";
import { Button } from "@/components/ui/Button";
import ManageActionIcon from "@/components/ui/ManageActionIcon";
import { useAuthStore } from "@/store/authStore";
import { getPublisherKind } from "@/lib/publishPermissions";

/**
 * The one action an event detail page offers.
 *
 * Events are announcements only. The creator gets Edit/Repost; everyone else
 * gets the organizer's own registration link, opened externally. There is no
 * in-app register/unregister — the organizer collects sign-ups on their website,
 * WhatsApp or Instagram, so Fursa never holds a participation record.
 *
 * Lifted out of `EventDetails` to bring that file back under the 200-line limit;
 * it was the largest self-contained piece and the only part of the screen that
 * is purely a function of its props.
 */
export default function EventDetailsActionButton({
  mobile = false,
  isCreator,
  isBanned,
  isRepostState,
  isActive,
  isCancelled = false,
  canEdit,
  registrationLink,
  onManage,
}: {
  mobile?: boolean;
  isCreator: boolean;
  isBanned?: boolean | null;
  isRepostState: boolean;
  isActive: boolean;
  isCancelled?: boolean;
  /**
   * BE-188 — false after the exact first-session instant minus 24 hours. Only read for Edit; a Repost creates a new event, so
   * it is never closed by this.
   */
  canEdit: boolean;
  registrationLink?: string | null;
  onManage: () => void;
}) {
  const { t } = useTranslation();
  const publishingPaused = useAuthStore(state => state.user?.is_publishing_paused === true);
  const isTeam = useAuthStore(state => getPublisherKind(state.user) === "team");

  if (isCreator) {
    /* Managing your own event isn't gated behind verification — the list cards
       never hid it for an unverified creator either. A banned user still can't. */
    if (isBanned) return null;
    /* X2: teams never publish events. A team's historical event stays as
       history; Edit/Repost would only lead to the form's refusal (the API
       answers 403 for both, `PublishPolicy::allows(…, 'event')`). */
    if (isTeam) return null;
    /* The shared icon row, same as the two opportunity detail screens. The
       desktop and mobile containers are mutually exclusive at the call site
       (`xss:hidden` vs `hidden xss:flex`), so one branch serves both. */
    return (
      <div
        className={
          mobile
            ? "flex flex-wrap gap-2"
            : "flex flex-wrap items-center justify-end gap-2"
        }
      >
        {/* BE-105 — disabled rather than gone: the organizer should read why
            the control is dead, not watch it vanish the day before. The label
            is what `title`/`aria-label` carry, so it is the explanation. */}
        <ManageActionIcon
          label={
            isRepostState
              ? t("COMMON.REPOST")
              : canEdit
                ? t("COMMON.EDIT_TEXT")
                : t("COMMON.EDIT_WINDOW_CLOSED")
          }
          icon={
            isRepostState ? (
              <RotateCcw className="h-5 w-5" />
            ) : (
              <Pencil className="h-5 w-5" />
            )
          }
          disabled={isRepostState ? publishingPaused : !canEdit}
          onClick={onManage}
        />
      </div>
    );
  }

  /* PDF p.13: «تسجيل» stays visible but disabled after the end; a cancelled event says so. */
  if (registrationLink && !isActive) {
    return (
      <Button
        size="medium"
        disabled
        aria-label={isCancelled ? undefined : `${t("COMMON.REGISTER")} — ${t("COMMON.ENDED")}`}
        className={mobile ? "my-6 !h-14 !w-full" : "whitespace-nowrap"}
      >
        <Lock className="me-2 h-4 w-4" aria-hidden="true" />
        {t(isCancelled ? "COMMON.CANCELLED" : "COMMON.REGISTER")}
      </Button>
    );
  }

  if (registrationLink && isActive) {
    return (
      <Button
        size="medium"
        className={mobile ? "my-6 !h-14 !w-full" : "whitespace-nowrap"}
        onClick={() =>
          window.open(registrationLink, "_blank", "noopener,noreferrer")
        }
      >
        {t("COMMON.REGISTER")}
      </Button>
    );
  }

  return null;
}
