/**
 * "Have I already reviewed this?" — including when the list is only partly
 * on screen.
 *
 * The component shows two reviews and hides the rest behind "show more". The
 * question these tests answer is whether the check is affected by that, and
 * the answer turns on a backend contract: `/opportunity-feedbacks/` ends in
 * `->get()`, so the client holds every review and the slicing is purely
 * visual.
 */
import { test, describe } from "node:test";
import assert from "node:assert/strict";
import {
  hasOwnEventFeedback,
  hasOwnFeedback,
} from "@/features/opportunities/components/ownFeedback";
import type { OpportunityFeedbackRecord } from "@/features/opportunities/permissionRecord";

const review = (userId: number, id = userId): OpportunityFeedbackRecord => ({
  id,
  learn_serve_opportunity_id: 9,
  user_id: userId,
  user_name: `User ${userId}`,
  rating: 5,
  comment_en: "Good",
  comment_ar: null,
  primary_language: "en",
  likes_count: 0,
  is_liked_by_me: false,
  created_at: null,
});

describe("hasOwnFeedback", () => {
  test("an anonymous viewer has never reviewed", () => {
    assert.equal(hasOwnFeedback([review(1), review(2)], undefined), false);
  });

  test("null and undefined lists are handled", () => {
    assert.equal(hasOwnFeedback(null, 1), false);
    assert.equal(hasOwnFeedback(undefined, 1), false);
  });

  test("finds the viewer's review", () => {
    assert.equal(hasOwnFeedback([review(1), review(7)], 7), true);
  });

  test("does not match someone else's", () => {
    assert.equal(hasOwnFeedback([review(1), review(2)], 7), false);
  });

  test("found even when it is outside the two reviews shown on screen", () => {
    // The component renders `feedbacks.slice(0, 2)` but asks this question of
    // the whole array. The endpoint sends every review in one response, so
    // "outside the visible slice" and "outside the loaded data" are not the
    // same thing — only the first is possible today.
    const list = [review(1), review(2), review(3), review(4), review(99)];
    assert.equal(list.slice(0, 2).some((f) => f.user_id === 99), false);
    assert.equal(hasOwnFeedback(list, 99), true);
  });

  test("a string id never matches a numeric user_id", () => {
    // `User["id"]` is a number and so is `user_id`; this pins that the
    // comparison is strict, so a future `string | number` id would have to
    // coerce rather than silently always-false.
    const loose = hasOwnFeedback([review(7)], "7" as unknown as number);
    assert.equal(loose, false);
  });
});


/**
 * The same question for an event's reviews, where the reviewer's id is nested
 * under `user` and is itself nullable.
 *
 * Same backend contract: `EventFeedbackController::index()` ends in `->get()`,
 * so "show all" slices a complete array and the check sees every review.
 */
describe("hasOwnEventFeedback", () => {
  const eventReview = (userId: number | null) => ({ user: { id: userId } });

  test("finds the viewer's own review", () => {
    assert.equal(hasOwnEventFeedback([eventReview(4), eventReview(7)], 7), true);
  });

  test("answers false when the viewer has not reviewed", () => {
    assert.equal(hasOwnEventFeedback([eventReview(4)], 7), false);
  });

  test("a signed-out viewer never matches", () => {
    assert.equal(hasOwnEventFeedback([eventReview(4)], undefined), false);
  });

  test("a review whose author was deleted does not match anyone", () => {
    /*
     * `$feedback->user?->id` is null for a review whose user row is gone. A
     * loose comparison, or one that treated null as "the current viewer",
     * would hide the review form from everyone on such an event.
     */
    assert.equal(hasOwnEventFeedback([eventReview(null)], 7), false);
  });

  test("an empty, null or undefined list answers false", () => {
    assert.equal(hasOwnEventFeedback([], 7), false);
    assert.equal(hasOwnEventFeedback(null, 7), false);
    assert.equal(hasOwnEventFeedback(undefined, 7), false);
  });

  test("the review outside the visible two is still found", () => {
    /*
     * The screen renders `feedbacks.slice(0, 2)` until "show all". The viewer's
     * review is third here, so this is the case that would break if the check
     * were ever asked of the visible slice instead of the whole list.
     */
    const all = [eventReview(1), eventReview(2), eventReview(7)];
    assert.equal(all.slice(0, 2).some((r) => r.user.id === 7), false);
    assert.equal(hasOwnEventFeedback(all, 7), true);
  });

  test("the removed `user_feedback` flag was never the deciding operand", () => {
    /*
     * The check used to be `feedback.user_feedback || feedback.user.id ===
     * viewer`. `user_feedback` appears nowhere in the backend, so it was
     * always `undefined` and the comparison decided every case — which is why
     * dropping it changes nothing. Asserted so a future "restore the flag"
     * change has to justify itself.
     */
    const withPhantomFlag: Array<{ user: { id: number | null } }> = [
      /* The extra key is what the row *used* to be read for; `tsc` now rejects
         it outright on the record, which is the point — hence the widening to
         `unknown` rather than a direct cast. */
      { user: { id: 4 }, user_feedback: undefined } as unknown as {
        user: { id: number | null };
      },
    ];
    assert.equal(hasOwnEventFeedback(withPhantomFlag, 7), false);
  });
});
