import type { OpportunityFeedbackRecord } from "@/features/opportunities/permissionRecord";

/**
 * Has this viewer already reviewed the opportunity?
 *
 * Decides whether the "share your review" affordance is offered, so a wrong
 * answer either hides the form from someone who may still review or offers it
 * to someone who cannot (the API refuses a second review).
 *
 * **It is answered over the whole list, and that is only sound because the
 * endpoint does not paginate.** `OpportunityFeedbackController::index()` ends
 * in `->get()`, not `->paginate()`: it reads no `page` or `limit`, emits no
 * `meta.pagination`, and returns every review for the opportunity in one
 * response. The "show more" control on the page slices that complete array
 * client-side, so the viewer's own review is in hand whether or not it is
 * currently on screen.
 *
 * If the backend ever paginates this endpoint, this check silently degrades to
 * "did the viewer review *within the loaded page*" — and there is no field on
 * the payload that would let the frontend recover the answer, because the
 * resource carries no "mine" flag (the `user_feedback` key the component used
 * to read does not exist). The honest fix then is a backend one: either a
 * top-level `has_own_feedback` on the envelope, or `user_feedback` on the row.
 * `tests/ownFeedback.test.ts` pins the current behaviour so the degradation is
 * visible rather than silent.
 *
 * `viewerId` is `User["id"]`, a number, and `user_id` is a number — compared
 * strictly on purpose. A string-vs-number `===` would always be false.
 */
export function hasOwnFeedback(
  feedbacks: readonly OpportunityFeedbackRecord[] | null | undefined,
  viewerId: number | undefined
): boolean {
  if (viewerId === undefined) return false;
  return (feedbacks ?? []).some((feedback) => feedback.user_id === viewerId);
}

/**
 * The same question for an **event's** reviews, where the reviewer's id is
 * nested.
 *
 * `WebsiteEventFeedbackResource` emits `user: { id, … }` rather than a flat
 * `user_id`, and `$feedback->user?->id` makes that id nullable — a review
 * whose author has since been deleted has `user.id === null`, which must not
 * match a viewer.
 *
 * Sound over the whole list for the same reason as the opportunity version:
 * `EventFeedbackController::index()` ends in `->get()`, reads no `page` or
 * `limit` and emits no `meta.pagination`, so the "show all" control on the
 * screen slices a complete array. The same degradation applies if that ever
 * changes, and the same backend fix would be needed.
 *
 * It replaces `feedback.user_feedback || feedback.user.id === user?.id`. The
 * first operand was always `undefined` — `user_feedback` appears nowhere in
 * the backend — so the comparison was carrying the whole test; dropping it
 * changes no behaviour and removes the suggestion that there is a server-side
 * flag to fall back on.
 */
export function hasOwnEventFeedback(
  feedbacks: readonly { user: { id: number | null } }[] | null | undefined,
  viewerId: number | undefined
): boolean {
  if (viewerId === undefined) return false;
  return (feedbacks ?? []).some((feedback) => feedback.user.id === viewerId);
}
