/**
 * Regression tests for the development-opportunity form's initial values.
 *
 * These cover the two behaviours that were changed while decomposing
 * `LearnServeForm`, plus the English-field rule that is easy to break because
 * it looks redundant.
 */
import { test, describe } from "node:test";
import assert from "node:assert/strict";
import { buildLearnServeInitialValues } from "@/features/opportunities/components/learnServeInitialValues";

const base = {
  selectedLanguage: "ar",
  hasDistinctEnglishTitle: false,
  hasDistinctEnglishDescription: false,
  tagOptions: [] as Array<{ id: string; label: string }>,
};

describe("buildLearnServeInitialValues", () => {
  test("coerces numeric master-choice ids to strings", () => {
    // `<SelectInput>` compares its option `value` — always a string — against
    // these. The API types `…_display.id` as `string | number`, so a numeric id
    // left uncoerced silently leaves the dropdown unset when editing.
    const values = buildLearnServeInitialValues({
      ...base,
      opportunityData: {
        learning_type_display: { id: 7 },
        format_display: { id: 3 },
        certificate_type_display: { id: 11 },
        gender_display: { id: 2 },
      },
    });

    assert.equal(values.learningType, "7");
    assert.equal(values.learnServeFormat, "3");
    assert.equal(values.certificateType, "11");
    assert.equal(values.gender, "2");
  });

  /*
   * Replaces a test that fed `{ id: "abc" }` and asserted it came through
   * unchanged. `BaseController::choices()` projects `master_choices.id`, an
   * auto-increment integer, so a string id is not a shape this code can
   * receive; the case that matters is that the integer is stringified, because
   * `buildLearnServePayload` compares `values.learningType` against the
   * dropdown's option value with `===`.
   */
  test("stringifies the numeric id the API actually sends", () => {
    const values = buildLearnServeInitialValues({
      ...base,
      opportunityData: { learning_type_display: { id: 7 } },
    });
    assert.equal(values.learningType, "7");
    assert.equal(typeof values.learningType, "string");
  });

  test("leaves every choice empty when the record has none", () => {
    const values = buildLearnServeInitialValues({ ...base, opportunityData: null });
    assert.equal(values.learningType, "");
    assert.equal(values.learnServeFormat, "");
    assert.equal(values.certificateType, "");
    assert.equal(values.gender, "");
  });

  test("preserves a legacy English title when editing", () => {
    // An opportunity published without opening the English panel stores the
    // Arabic text in both columns. Prefilling from it would put Arabic into a
    // field labelled English the moment the panel is opened.
    const values = buildLearnServeInitialValues({
      ...base,
      hasDistinctEnglishTitle: false,
      hasDistinctEnglishDescription: false,
      opportunityData: {
        title_ar: "عنوان",
        title_en: "عنوان",
        description_ar: "<p>وصف</p>",
        description_en: "<p>وصف</p>",
      },
    });

    assert.equal(values.title_ar, "عنوان");
    assert.equal(values.title_en, "عنوان");
    assert.equal(values.description_en, "");
  });

  test("preserves the independent English title while descriptions use single-content authoring", () => {
    const values = buildLearnServeInitialValues({
      ...base,
      hasDistinctEnglishTitle: true,
      hasDistinctEnglishDescription: true,
      opportunityData: {
        title_ar: "عنوان",
        title_en: "Title",
        description_ar: "<p>وصف</p>",
        description_en: "<p>Description</p>",
      },
    });

    assert.equal(values.title_en, "Title");
    assert.equal(values.description_en, "");
  });

  test("starts a new form with one empty sponsor row", () => {
    const values = buildLearnServeInitialValues({ ...base, opportunityData: null });
    assert.deepEqual(values.sponsors, [{ sponsorId: "", position: 1 }]);
  });

  test("maps saved sponsors to their organization ids, in order", () => {
    const values = buildLearnServeInitialValues({
      ...base,
      opportunityData: {
        opportunity_sponsor_images: [
          { organization: { id: 12 } },
          { organization: 34 },
        ],
      },
    });

    assert.deepEqual(values.sponsors, [
      { sponsorId: "12", position: 1 },
      { sponsorId: "34", position: 2 },
    ]);
  });
});
