import { hasOpportunityStarted, hasOpportunityEnded, isOpportunityCancelled, parseKuwaitInstant } from "@/features/shared/clientReviewPolicy";
import moment from "moment";
import { toNumber } from "@/lib/helpers";
import { canEditOrDelete } from "@/features/shared/opportunityButtonState";
import { certificatesNeedSending, type CertificatesOutdatedSource } from "@/features/opportunities/certificatesOutdated";
import { awaitsApproval } from "@/features/opportunities/components/volunteerListHelpers";
import {
  getCheckInWindow,
  type CheckInWindow,
} from "@/features/opportunities/checkInWindow";
import {
  canToggleRegistration,
  getOpportunityButtonLabelKey,
  getOpportunityButtonState,
  isOpportunityButtonActionable,
  isViewerOrganizer,
  type OpportunityButtonState,
} from "@/features/shared/opportunityButtonState";
import type {
  OpportunityCreator,
  VolunteerOpportunityDetail,
} from "./types";

/**
 * Every rule that decides what the volunteer-opportunity page shows, as one
 * pure function of the payload and the viewer.
 *
 * These used to be forty-odd `const`s between the component's loading early
 * return and its JSX. That placement is what made the page fragile: anything
 * written there with a hook in it is a *conditional* hook, which is exactly how
 * a `useMemo` crashed the page with "Rendered more hooks than during the
 * previous render". A plain function cannot have that bug, and can be read —
 * or tested — without rendering anything.
 */

/** Who is looking. Only the handful of fields the rules actually read. */
export interface VolunteerEventViewer {
  id?: number | string | null;
  /** A user record is present in the auth store. */
  signedIn: boolean;
  /** …and it carries a token. */
  hasAuthToken: boolean;
  isVerified: boolean;
  userType?: string | null;
}

export interface VolunteerEventView {
  isCreator: boolean;
  isCompleted: boolean;
  canEdit: boolean;
  canMessage: boolean;
  isRejected: boolean;

  /** Registration was shut by hand, or the backend reports the window shut. */
  closedByCreator: boolean;
  showRegistrationClosedBadge: boolean;
  isRegistrationClosed: boolean;
  isRepostState: boolean;

  /** The viewer's register / unregister button, or the creator's Edit / Repost. */
  showActionButton: boolean;
  actionLabelKey: string;
  /** Started / Ended / Full / Closed are states, not actions. */
  isViewerActionDisabled: boolean;
  viewerButtonState: OpportunityButtonState;

  canCloseRegistration: boolean;
  canReopenRegistration: boolean;
  canRequestDeletion: boolean;
  /**
   * The «Request deletion» review is still on offer. Server `can_request_deletion`
   * first (false once a request is pending); absent on older payloads, where the
   * started/paid fallbacks inside `DeletionReviewAction` decide.
   */
  canRequestDeletionReview: boolean;
  canSendCertificates: boolean;
  /** A15: the window is open but nothing changed since the automatic issuance. */
  certificatesUpToDate: boolean;
  certificatesOutdated: CertificatesOutdatedSource | null;
  /** Completed, but nobody was checked in — the glyph shows, disabled. */
  certificatesNeedAttendee: boolean;

  checkInWindow: CheckInWindow;
  canManageAttendance: boolean;
  canGenerateAttendanceQr: boolean;
}

/** Strict ISO parse, UTC. Null for absent or unparseable input. */
export function parseIsoUtcDate(value?: string | null): moment.Moment | null {
  if (!value) return null;
  const parsed = moment.utc(value, moment.ISO_8601, true);
  return parsed.isValid() ? parsed : null;
}

export function deriveVolunteerEventView(
  data: VolunteerOpportunityDetail | undefined,
  viewer: VolunteerEventViewer,
  now: moment.Moment = moment.utc()
): VolunteerEventView {
  // `/opportunities/{id}/details/` reports ownership through
  // `relationship_tags` and no longer sends `is_creator`, so the check ORs
  // every signal — see `isViewerOrganizer`.
  const isCreator = isViewerOrganizer(data, viewer.id);

  const isCompleted = hasOpportunityEnded(data, now);
  const viewerButtonState = getOpportunityButtonState(data, now);
  const closedByCreator = data?.is_registration_closed === true || data?.is_registration_open === false;
  const registrationDeadline = parseKuwaitInstant(data?.registration_closes_at)
    ?? parseKuwaitInstant(data?.due_date)?.endOf("day");
  const showRegistrationClosedBadge = closedByCreator && !isCompleted && !isCreator &&
    (!registrationDeadline || now.isBefore(registrationDeadline));
  const hasStarted = hasOpportunityStarted(data, now);
  const isRegistrationClosed = closedByCreator || ["closed", "unavailable", "full"].includes(viewerButtonState) ||
    (hasStarted && data?.can_register_while_running !== true);

  // Server-computed first; `isAtCapacity()` is exactly the rule below, which
  // stays only for a payload that omits the flag.
  // `action_state` is authoritative whenever present — and the detail endpoint
  // always sends it. The fallback covers only a payload that predates it, and
  // prefers the server's `has_started` over a start-of-day guess that reads a
  // same-day opportunity as started from midnight.
  const isRepostState = isCreator && hasStarted;

  // The creator's own Edit / Repost is never gated on verification — the list
  // cards never hid it either. Only a viewer's register action is.
  const showActionButton = isCreator || viewer.userType !== "organization";

  // Volunteering registration may be reopened while the opportunity is running.
  const canManageRegistration =
    isCreator && !isCompleted && canToggleRegistration(data, now);
  const canCloseRegistration = canManageRegistration && !isRegistrationClosed;
  const canReopenRegistration =
    canManageRegistration && isRegistrationClosed;

  const isRejected = data?.approval_status === "rejected";
  // Matches the cards: deletion is requested before the opportunity starts.
  const canRequestDeletion =
    isCreator;
  // Completion issues certificates for everyone already marked attended; this
  // is for attendance recorded after that pass, and is safe to repeat (BE-14).
  const certificatesWindowOpen = isCreator && isCompleted && !getCheckInWindow(data, now).isClosed;
  const certificatesOutdated = (data ?? null) as CertificatesOutdatedSource | null;
  const certificatesUpToDate = certificatesWindowOpen && !certificatesNeedSending(certificatesOutdated);
  const canSendCertificates = certificatesWindowOpen && !certificatesUpToDate;
  /*
   * «لا يتفعل إلا إذا تم تحضير شخص واحد على الأقل» (client, 2026-10-10).
   * BE-121 asks for `attended_count` on the detail payload; until it arrives
   * the glyph stays pressable and the server decides.
   */
  const attendedCount =
    data?.attended_count == null ? null : Number(data.attended_count);
  const certificatesNeedAttendee =
    canSendCertificates && attendedCount !== null && attendedCount === 0;

  const actionLabelKey = isRejected
    ? "COMMON.EDIT_AND_RESUBMIT"
    : isRepostState
      ? "COMMON.REPOST"
      : isCreator
        ? "COMMON.EDIT_TEXT"
        : viewerButtonState === "register" && !viewer.hasAuthToken
          ? "COMMON.REGISTER_NOW"
          : getOpportunityButtonLabelKey(viewerButtonState);

  const isViewerActionDisabled =
    !isRejected &&
    !isRepostState &&
    !isCreator &&
    !isOpportunityButtonActionable(viewerButtonState);

  /*
   * The attendance window, its deadline and whether it is open all come from
   * the backend (default 72h past the end date, admin-editable). QR and manual
   * attendance run side by side.
   */
  const checkInWindow = getCheckInWindow(data, now);

  return {
    isCreator,
    isCompleted,
    canEdit: isCreator && canEditOrDelete(data, now),
    // Server `can_message` first: it stays true after an administration
    // cancellation that kept attendance (PDF p.40), which ends the opportunity.
    canMessage: isCreator && (data?.can_message ?? (!isCompleted && !isOpportunityCancelled(data))),
    isRejected,
    closedByCreator,
    showRegistrationClosedBadge,
    isRegistrationClosed,
    isRepostState,
    showActionButton,
    actionLabelKey,
    isViewerActionDisabled,
    viewerButtonState,
    canCloseRegistration,
    canReopenRegistration,
    canRequestDeletion,
    canRequestDeletionReview: isCreator && data?.can_request_deletion !== false,
    canSendCertificates,
    certificatesUpToDate,
    certificatesOutdated,
    certificatesNeedAttendee,
    checkInWindow,
    /*
     * BE-69 — the volunteer list opens to the organizer *and* a volunteer they
     * granted «إذن تحضير». Only the list: every other control in that row
     * keeps its own `isCreator` gate.
     */
    canManageAttendance: isCreator || data?.can_manage_attendance === true,
    // The same daily arrival/departure code remains printable after completion,
    // but not before approval: the server refuses those scans (BE-258).
    canGenerateAttendanceQr: isCreator && !awaitsApproval(data?.approval_status),
  };
}

// ─── Presentation helpers ───────────────────────────────────────────────────
// Pure reads of the payload, shared by the section components so each can
// render from `data` alone.

/** Title in the opportunity's own `primary_language`, as the heading uses. */
export function opportunityTitle(data?: VolunteerOpportunityDetail): string {
  return (
    (data?.primary_language === "ar" ? data?.title_ar : data?.title_en) || ""
  );
}

export function organizerProfilePath(creator?: OpportunityCreator | null): string {
  return `/public-profile/${creator?.id}`;
}

/*
 * An opportunity can answer "where" with a maps link instead of a pin. The API
 * used to fall `location_url` back to `link` — the WhatsApp number — so an empty
 * location opened a WhatsApp chat when someone clicked the address. BE-60 fixed
 * that server-side; the guard stays because it costs nothing and the failure
 * mode is embarrassing.
 */
export function resolveLocationUrl(data?: VolunteerOpportunityDetail): string | null {
  return data?.location_url && data.location_url !== data?.link
    ? data.location_url
    : null;
}

/** The exact days a separate-days opportunity runs, ascending and de-duplicated. */
export function scheduledDaysOf(data?: VolunteerOpportunityDetail): string[] {
  const slots = data?.time_slots;
  if (!Array.isArray(slots)) return [];
  return [
    ...new Set(
      slots
        .map((slot) => slot?.date?.slice(0, 10))
        .filter((day): day is string => Boolean(day))
    ),
  ].sort();
}

/** Length of one start→end shift in hours, rolling an overnight shift forward. */
function shiftHours(start?: string | null, end?: string | null): number {
  if (!start || !end) return 0;
  const hours = moment
    .duration(moment(end, "HH:mm:ss").diff(moment(start, "HH:mm:ss")))
    .asHours();
  // 21:00 → 01:00 is four hours, not minus twenty. Mirrors the backend's
  // `VolunteerOpportunityTimeSlot::durationInHours()`.
  return hours < 0 ? hours + 24 : hours;
}

/**
 * Total commitment, as whole hours and remaining minutes.
 *
 * For a separate-days opportunity the backend already computes each slot's
 * length (`time_slots[].hours`), so those are summed — which is also the only
 * reading that stays right if the days ever run different hours. A consecutive
 * range is hours-per-day × the days in it.
 */
export function totalCommitment(data?: VolunteerOpportunityDetail): {
  hrs: number;
  mins: number;
} {
  if (!data) return { hrs: 0, mins: 0 };

  const slots = data.time_slots ?? [];
  const slotHours = slots.map((slot) => slot?.hours);
  const serverSummable =
    slots.length > 0 && slotHours.every((h) => typeof h === "number");

  let totalHours: number;
  if (serverSummable) {
    totalHours = (slotHours as number[]).reduce((sum, h) => sum + h, 0);
  } else {
    const days = scheduledDaysOf(data);
    const numberOfDays =
      days.length > 0
        ? days.length
        : moment(data.end_date).diff(moment(data.start_date), "days") + 1;
    totalHours = shiftHours(data.start_time, data.end_time) * numberOfDays;
  }

  const hrs = Math.floor(totalHours);
  return { hrs, mins: Math.round((totalHours - hrs) * 60) };
}

/** Places still open; never negative. */
export function remainingPlaces(data?: VolunteerOpportunityDetail): number {
  return Math.max(
    0,
    toNumber(data?.participants_needed) -
      toNumber(data?.registered_volunteers_count)
  );
}
