/**
 * Seeding the volunteer-role form from the saved role.
 *
 * Every assertion here is a response-contract detail the previous local
 * `Role` type's index signature hid: the localized columns are a fixed
 * `_en`/`_ar` pair, all four are nullable, and `participants_needed` is a
 * number on the payload but a string in the form.
 */
import { test, describe } from "node:test";
import assert from "node:assert/strict";
import { roleFormInitialValues } from "@/features/opportunities/components/roleFormInitialValues";
import type { VolunteerOpportunityRoleRecord } from "@/features/opportunities/rolesAndTeamsRecord";

const role = (
  over: Partial<VolunteerOpportunityRoleRecord> = {}
): VolunteerOpportunityRoleRecord => ({
  id: 1,
  // `opportunity_id`, not `opportunity` — the time-slot resource uses the other name.
  opportunity_id: 9,
  role_name_en: "Usher",
  role_name_ar: "مرشد",
  instructions_en: "Greet arrivals",
  instructions_ar: "استقبال",
  participants_needed: 4,
  assigned_count: 0,
  remaining_slots: 4,
  is_full: false,
  ...over,
});

describe("roleFormInitialValues", () => {
  test("adding: the form opens pre-filled with the outstanding count", () => {
    const v = roleFormInitialValues({
      savedRole: undefined,
      isEditing: false,
      language: "en",
      participantsStillNeeded: 7,
    });
    assert.deepEqual(v, {
      role_name: "",
      instructions: "",
      participants_needed: "7",
    });
  });

  test("editing before the fetch resolves leaves the count blank, not pre-filled", () => {
    // Pre-filling with the shortfall while editing would silently rewrite the
    // role's own number the moment the organiser pressed save.
    const v = roleFormInitialValues({
      savedRole: undefined,
      isEditing: true,
      language: "en",
      participantsStillNeeded: 7,
    });
    assert.equal(v.participants_needed, "");
  });

  test("the language picks the matching pair", () => {
    const en = roleFormInitialValues({
      savedRole: role(),
      isEditing: true,
      language: "en",
      participantsStillNeeded: 0,
    });
    const ar = roleFormInitialValues({
      savedRole: role(),
      isEditing: true,
      language: "ar",
      participantsStillNeeded: 0,
    });
    assert.equal(en.role_name, "Usher");
    assert.equal(ar.role_name, "مرشد");
    assert.equal(ar.instructions, "استقبال");
  });

  test("an unrecognised language falls back to English rather than undefined", () => {
    // The old `role[`role_name_${language}`]` read produced `undefined` here.
    const v = roleFormInitialValues({
      savedRole: role(),
      isEditing: true,
      language: "fr",
      participantsStillNeeded: 0,
    });
    assert.equal(v.role_name, "Usher");
  });

  test("a null name renders empty, never the string \"null\"", () => {
    const v = roleFormInitialValues({
      savedRole: role({ role_name_ar: null, instructions_ar: null }),
      isEditing: true,
      language: "ar",
      participantsStillNeeded: 0,
    });
    assert.equal(v.role_name, "");
    assert.equal(v.instructions, "");
  });

  test("participants_needed arrives as a number and leaves as a string", () => {
    const v = roleFormInitialValues({
      savedRole: role({ participants_needed: 12 }),
      isEditing: true,
      language: "en",
      participantsStillNeeded: 3,
    });
    assert.equal(v.participants_needed, "12");
  });

  test("a saved 0 is kept — `||` would have replaced it with the shortfall", () => {
    const v = roleFormInitialValues({
      savedRole: role({ participants_needed: 0 }),
      isEditing: true,
      language: "en",
      participantsStillNeeded: 5,
    });
    assert.equal(v.participants_needed, "0");
  });

  test("a null participants_needed falls back rather than becoming \"null\"", () => {
    const v = roleFormInitialValues({
      savedRole: role({ participants_needed: null }),
      isEditing: true,
      language: "en",
      participantsStillNeeded: 5,
    });
    assert.equal(v.participants_needed, "");
  });
});
