import moment from "moment";
import { NAV_STATE_KEYS, setNavState } from "@/lib/navigationState";
import type { VolunteerOpportunityDetail } from "./types";
import type { CheckInWindow } from "@/features/opportunities/checkInWindow";

/**
 * The payload `/volunteerlist` reads on mount.
 *
 * App Router has no `navigate(path, { state })`, so the contract between this
 * page and that one is this object and nothing else — which is exactly why it
 * does not belong inline in a panel's click handler. Every field here is one
 * the volunteer list cannot work without and cannot re-derive.
 */
export function stashVolunteerListNavState(
  data: VolunteerOpportunityDetail,
  checkInWindow: CheckInWindow,
  isCreator: boolean
): void {
  // `requires_check_in` used to ride along too, but the detail endpoint never
  // sends it for volunteering, and `setNavState` JSON-encodes — an undefined
  // key was already being dropped.
  setNavState(NAV_STATE_KEYS.registerList, {
    id: data.id,
    opportunity_status: data.opportunity_status,
    // Manual attendance runs alongside QR; the backend owns the deadline.
    manual_tracking: checkInWindow.manualEnabled && checkInWindow.isOpen,
    qr_attendance_enabled: data.qr_attendance_enabled,
    manual_attendance_enabled: data.manual_attendance_enabled,
    preparation_valid_until: data.preparation_valid_until,
    preparation_valid_until_at: data.preparation_valid_until_at,
    is_preparation_window_closed: data.is_preparation_window_closed,
    preparation_reopened_until: data.preparation_reopened_until,
    // Deleting registrations locks from the start *day*. Deliberately not the
    // server's `has_started`, which flips at the start *time* — that would
    // reopen deletion for the morning of the first day.
    disableDeleteAfterPeriod: moment().isAfter(
      moment(data.start_date).startOf("day")
    ),
    start_at: data.start_at,
    end_at: data.end_at,
    registration_closes_at: data.registration_closes_at,
    start_date: data.start_date,
    end_date: data.end_date,
    start_time: data.start_time,
    end_time: data.end_time,
    time_slots: data.time_slots,
    participants_needed: data.participants_needed,
    // BE-69 — who is looking. `can_manage_attendance` covers the organizer and
    // a granted volunteer; `is_creator` separates them, since only the
    // organizer may grant the permission on.
    is_creator: isCreator,
    can_manage_attendance: data.can_manage_attendance ?? isCreator,
    approval_status: data.approval_status ?? null,
  });
}

/**
 * Read, and nothing was stashed: `/volunteerlist` opened by URL or in a tab
 * that never visited the detail page. The id travels only in this payload, so
 * the screen cannot load anything and must say so instead of rendering an
 * empty roster that reads as "no volunteers yet". `null` is "not read yet".
 */
export function isVolunteerListSourceMissing(
  navState: { id?: string | number | null } | null
): boolean {
  return navState !== null && (navState.id == null || navState.id === "");
}
