/**
 * Regression tests for the registration list's bulk-selection rules.
 *
 * Attendance is marked in bulk over an infinitely scrolling list, so "select
 * all" has to keep applying to rows nobody has seen yet — and has to stop the
 * moment the organizer unticks one. Both halves are easy to get wrong and
 * neither shows up as a type error; getting the second one wrong silently marks
 * volunteers present who were deliberately excluded.
 */
import { test, describe } from "node:test";
import assert from "node:assert/strict";
import {
  EMPTY_SELECTION,
  effectiveSelectedUsers,
  idsToAutoSelect,
  selectableIds,
  toggleRegistration,
  toggleSelectAll,
  type SelectableRegistration,
} from "@/features/opportunities/components/registrationSelection";

const rows = (...spec: Array<[number, boolean]>): SelectableRegistration[] =>
  spec.map(([id, is_attended]) => ({ id, is_attended }));

describe("selectableIds", () => {
  test("excludes rows already marked present", () => {
    // Their checkbox is ticked and disabled; re-sending them would be a no-op
    // the server still has to process.
    assert.deepEqual(selectableIds(rows([1, false], [2, true], [3, false])), ["1", "3"]);
  });

  test("ids come back as strings, because that is what the state holds", () => {
    assert.deepEqual(selectableIds(rows([7, false])), ["7"]);
  });
});

describe("toggleSelectAll", () => {
  test("selects every loaded row, not just the visible page", () => {
    const next = toggleSelectAll(rows([1, false], [2, false], [3, false]), EMPTY_SELECTION);
    assert.deepEqual(next.selectedUsers, ["1", "2", "3"]);
    assert.equal(next.selectAll, true);
    // In force, so pages loaded afterwards are ticked too.
    assert.equal(next.selectAllMode, true);
  });

  test("skips rows already marked present", () => {
    const next = toggleSelectAll(rows([1, false], [2, true]), EMPTY_SELECTION);
    assert.deepEqual(next.selectedUsers, ["1"]);
  });

  test("unticking it clears everything, including the mode", () => {
    const selected = toggleSelectAll(rows([1, false], [2, false]), EMPTY_SELECTION);
    const cleared = toggleSelectAll(rows([1, false], [2, false]), selected);
    assert.deepEqual(cleared, EMPTY_SELECTION);
  });
});

describe("toggleRegistration", () => {
  const three = rows([1, false], [2, false], [3, false]);

  test("ticking one adds it", () => {
    const next = toggleRegistration(1, three, EMPTY_SELECTION);
    assert.deepEqual(next.selectedUsers, ["1"]);
    assert.equal(next.selectAll, false);
  });

  test("ticking the last one lights up the header box", () => {
    let state = toggleRegistration(1, three, EMPTY_SELECTION);
    state = toggleRegistration(2, three, state);
    assert.equal(state.selectAll, false);
    state = toggleRegistration(3, three, state);
    assert.equal(state.selectAll, true);
  });

  test("UNTICKING one cancels select-all mode", () => {
    // The critical rule: the organizer has just said "not that one", so rows
    // that load afterwards must not be ticked on their behalf.
    const all = toggleSelectAll(three, EMPTY_SELECTION);
    assert.equal(all.selectAllMode, true);
    const after = toggleRegistration(2, three, all);
    assert.equal(after.selectAllMode, false);
    assert.equal(after.selectAll, false);
    assert.deepEqual(after.selectedUsers, ["1", "3"]);
  });

  test("re-ticking by hand does NOT restore the mode", () => {
    // Only the header checkbox puts select-all back in force.
    const all = toggleSelectAll(three, EMPTY_SELECTION);
    const unticked = toggleRegistration(2, three, all);
    const reticked = toggleRegistration(2, three, unticked);
    assert.equal(reticked.selectAll, true);
    assert.equal(reticked.selectAllMode, false);
  });

  test("an already-attended row does not block the header box", () => {
    // It counts as satisfied, so selecting the one selectable row is "all".
    const mixed = rows([1, false], [2, true]);
    const next = toggleRegistration(1, mixed, EMPTY_SELECTION);
    assert.equal(next.selectAll, true);
  });

  test("a list with nothing selectable never lights up the header box", () => {
    const allAttended = rows([1, true], [2, true]);
    const next = toggleRegistration(1, allAttended, EMPTY_SELECTION);
    assert.equal(next.selectAll, false);
  });
});

describe("idsToAutoSelect", () => {
  test("returns the new page's selectable ids", () => {
    assert.deepEqual(idsToAutoSelect(rows([4, false], [5, false]), ["1"]), ["4", "5"]);
  });

  test("never duplicates an id already selected", () => {
    // Without this the selected list grows on every re-delivery of a page and
    // the same registration is sent twice.
    assert.deepEqual(idsToAutoSelect(rows([4, false], [5, false]), ["4"]), ["5"]);
  });

  test("skips already-attended rows on the new page", () => {
    assert.deepEqual(idsToAutoSelect(rows([4, true], [5, false]), []), ["5"]);
  });
});

/**
 * Whether "select all" keeps applying to pages that load afterwards.
 *
 * That used to be arranged by an `onPageMerged(newRows)` callback the paging
 * hook invoked per merged page, reached through a ref assigned in an effect.
 * It is now derived, so these pin the derivation against the two toggles that
 * can set or clear the mode.
 */
describe("effectiveSelectedUsers", () => {
  test("with select-all off, it is exactly what was ticked", () => {
    const state = { ...EMPTY_SELECTION, selectedUsers: ["2"] };
    assert.deepEqual(
      effectiveSelectedUsers(state, rows([1, false], [2, false])),
      ["2"]
    );
  });

  test("with select-all on, rows loaded later are included", () => {
    // The mode went on when only row 1 was loaded.
    const afterToggle = toggleSelectAll(rows([1, false]), EMPTY_SELECTION);
    assert.deepEqual(afterToggle.selectedUsers, ["1"]);
    // Page two arrives.
    assert.deepEqual(
      effectiveSelectedUsers(
        afterToggle,
        rows([1, false], [2, false], [3, false])
      ),
      ["1", "2", "3"]
    );
  });

  test("already-attended rows are never selected", () => {
    const state = toggleSelectAll(rows([1, false]), EMPTY_SELECTION);
    assert.deepEqual(
      effectiveSelectedUsers(state, rows([1, false], [2, true], [3, false])),
      ["1", "3"]
    );
  });

  test("unticking one leaves the mode, so later pages are NOT auto-selected", () => {
    const loaded = rows([1, false], [2, false]);
    const all = toggleSelectAll(loaded, EMPTY_SELECTION);
    const afterUntick = toggleRegistration(2, loaded, all);
    assert.equal(afterUntick.selectAllMode, false);
    assert.deepEqual(
      effectiveSelectedUsers(afterUntick, rows([1, false], [2, false], [3, false])),
      ["1"]
    );
  });

  test("turning select-all off selects nothing, however many rows load", () => {
    const on = toggleSelectAll(rows([1, false]), EMPTY_SELECTION);
    const off = toggleSelectAll(rows([1, false]), on);
    assert.equal(off.selectAllMode, false);
    assert.deepEqual(effectiveSelectedUsers(off, rows([1, false], [2, false])), []);
  });

  test("an empty list selects nothing even with the mode on", () => {
    const on = { ...EMPTY_SELECTION, selectAllMode: true, selectAll: true };
    assert.deepEqual(effectiveSelectedUsers(on, []), []);
  });
});
