/**
 * The volunteer-role create/update body, from a form with one name field.
 *
 * `VolunteerOpportunityRoleController::store()` requires `role_name_en` **and**
 * `role_name_ar`, but the editor has a single name box and a single
 * instructions box. What fills the other language is the rule under test.
 *
 * It is also the body that used to be built with computed keys
 * (`role_name_${lang}`), which gave the object an index signature — and an
 * index signature satisfies any required-key check, which is exactly how a
 * wrong key reached `POST /opportunity-feedbacks/` unnoticed for the life of
 * that feature.
 */
import { test, describe } from "node:test";
import assert from "node:assert/strict";
import { volunteerRoleRequestBody } from "@/features/opportunities/components/roleRequestBody";
import type { VolunteerOpportunityRoleRecord } from "@/features/opportunities/rolesAndTeamsRecord";

const saved = (
  overrides: Partial<VolunteerOpportunityRoleRecord> = {}
): VolunteerOpportunityRoleRecord =>
  ({
    id: 3,
    opportunity_id: 9,
    role_name_en: "Stored English",
    role_name_ar: "المخزن بالعربية",
    instructions_en: "Stored instructions",
    instructions_ar: "تعليمات مخزنة",
    participants_needed: 4,
    ...overrides,
  }) as VolunteerOpportunityRoleRecord;

const base = {
  roleName: "Typed",
  instructions: "Typed instructions",
  participantsNeeded: "3",
  opportunityId: "9",
} as const;

describe("volunteerRoleRequestBody — creating", () => {
  test("typing in English duplicates into Arabic, and vice versa", () => {
    const english = volunteerRoleRequestBody({ ...base, language: "en" });
    assert.equal(english.role_name_en, "Typed");
    assert.equal(english.role_name_ar, "Typed");

    const arabic = volunteerRoleRequestBody({ ...base, language: "ar" });
    assert.equal(arabic.role_name_ar, "Typed");
    assert.equal(arabic.role_name_en, "Typed");
  });

  test("both required names are always present", () => {
    /* The endpoint validates both as `required`; sending one is a 422. */
    for (const language of ["en", "ar"] as const) {
      const body = volunteerRoleRequestBody({ ...base, language });
      assert.equal(typeof body.role_name_en, "string");
      assert.equal(typeof body.role_name_ar, "string");
      assert.notEqual(body.role_name_en, "");
      assert.notEqual(body.role_name_ar, "");
    }
  });

  test("the ids are numbers, because the endpoint validates `integer`", () => {
    const body = volunteerRoleRequestBody({ ...base, language: "en" });
    assert.equal(body.opportunity_id, 9);
    assert.equal(body.participants_needed, 3);
  });
});

describe("volunteerRoleRequestBody — editing", () => {
  test("the untouched language keeps what was stored", () => {
    const body = volunteerRoleRequestBody({
      ...base,
      language: "en",
      savedRole: saved(),
    });
    assert.equal(body.role_name_en, "Typed");
    assert.equal(body.role_name_ar, "المخزن بالعربية");
    assert.equal(body.instructions_en, "Typed instructions");
    assert.equal(body.instructions_ar, "تعليمات مخزنة");
  });

  test("editing in Arabic keeps the stored English", () => {
    const body = volunteerRoleRequestBody({
      ...base,
      language: "ar",
      savedRole: saved(),
    });
    assert.equal(body.role_name_ar, "Typed");
    assert.equal(body.role_name_en, "Stored English");
  });

  test("a stored name that is null falls back to the typed one", () => {
    /*
     * Both columns are nullable on the read side — an admin or a legacy row
     * can produce a null — and the endpoint still requires both on write. So
     * the typed value has to cover a missing stored one rather than sending
     * `null` and taking a 422.
     */
    const body = volunteerRoleRequestBody({
      ...base,
      language: "en",
      savedRole: saved({ role_name_ar: null, instructions_ar: null }),
    });
    assert.equal(body.role_name_ar, "Typed");
    assert.equal(body.instructions_ar, "Typed instructions");
  });

  test("an empty stored name falls back too, not just a null one", () => {
    const body = volunteerRoleRequestBody({
      ...base,
      language: "en",
      savedRole: saved({ role_name_ar: "" }),
    });
    assert.equal(body.role_name_ar, "Typed");
  });

  test("the body carries no key the endpoint does not accept", () => {
    /*
     * `update()` takes the four text fields and the count; it does **not**
     * accept `opportunity_id` (a role cannot be moved between
     * opportunities), and `createVolunteerOpportunityRole` is the only caller
     * that sends one. Asserted as an exact key set so an added field has to
     * be checked against the validator.
     */
    const body = volunteerRoleRequestBody({
      ...base,
      language: "en",
      savedRole: saved(),
    });
    assert.deepEqual(Object.keys(body).sort(), [
      "instructions_ar",
      "instructions_en",
      "opportunity_id",
      "participants_needed",
      "role_name_ar",
      "role_name_en",
    ]);
  });
});
