import { describe, expect, test } from "vitest";
import { SURVEY_IMPORT_MAX_NOTE_CHARS } from "../limits";
import { loadQsfFixture } from "./__fixtures__/load-fixture";
import { loadRecordedPlan } from "./__fixtures__/recorded-plans";
import {
  checkPlanResponses,
  checkQuestionRoles,
  mergeCheckedPlans,
  ratingRangeFor,
  sliderPoints,
} from "./plan-checks";
import type { TQsfPlanQuestion } from "./plan-schema";
import { readQsf } from "./read-qsf";

const simple = readQsf(loadQsfFixture("simple.qsf"));
const advanced = readQsf(loadQsfFixture("matrix-slider-ranking.qsf"));
const logic = readQsf(loadQsfFixture("logic-skip-display-branch.qsf"));

const entry = (ref: string, type: string, fields: Partial<TQsfPlanQuestion> = {}): TQsfPlanQuestion => ({
  ref,
  type,
  required: false,
  choicesFrom: null,
  rowsFrom: null,
  columnsFrom: null,
  otherChoiceKey: null,
  noneChoiceKey: null,
  labelKey: null,
  excludedKeys: [],
  contactFields: [],
  inputType: null,
  scale: null,
  range: null,
  format: null,
  logicNotes: [],
  ...fields,
});

const question = (survey: typeof simple, ref: string) => {
  const found = survey.questions.get(ref);
  if (!found) throw new Error(`No ${ref}`);
  return found;
};

const failureOf = (result: ReturnType<typeof checkQuestionRoles>) => (result.ok ? [] : result.reasons);

describe("checkQuestionRoles", () => {
  test("accepts a multiple choice question with its other and none options", () => {
    const result = checkQuestionRoles(
      question(simple, "QID5"),
      entry("QID5", "multipleChoiceMulti", { choicesFrom: "choices", noneChoiceKey: "c18" })
    );

    expect(result.ok && result.question.choices).toEqual([
      { key: "c15" },
      { key: "c16" },
      { key: "c17" },
      { key: "c18", special: "none" },
    ]);
  });

  test.each(["pictureSelection", "cal", "consent2", ""])("refuses the type %j", (type) => {
    expect(failureOf(checkQuestionRoles(question(simple, "QID1"), entry("QID1", type)))).toEqual([
      "type_not_allowed",
    ]);
  });

  test("refuses a key that belongs to another question", () => {
    // c4 is QID2's.
    const result = checkQuestionRoles(
      question(simple, "QID1"),
      entry("QID1", "multipleChoiceSingle", { choicesFrom: "choices", otherChoiceKey: "c4" })
    );

    expect(failureOf(result)).toEqual(["foreign_key"]);
  });

  test("refuses a key used for two roles", () => {
    const result = checkQuestionRoles(
      question(simple, "QID1"),
      entry("QID1", "multipleChoiceSingle", {
        choicesFrom: "choices",
        otherChoiceKey: "c3",
        excludedKeys: ["c3"],
      })
    );

    expect(failureOf(result)).toEqual(["key_reused"]);
  });

  test("refuses an other option the question's choice list does not hold", () => {
    const matrix = question(advanced, "QID1");
    const result = checkQuestionRoles(
      matrix,
      entry("QID1", "multipleChoiceSingle", { choicesFrom: "choices", otherChoiceKey: "a1" })
    );

    expect(failureOf(result)).toEqual(["foreign_key"]);
  });

  test.each([
    ["a choice type without a choice list", "QID1", entry("QID1", "multipleChoiceSingle"), "missing_role"],
    [
      "a role its type does not use",
      "QID3",
      entry("QID3", "openText", { choicesFrom: "choices" }),
      "role_not_for_type",
    ],
    [
      "a none option on a single choice",
      "QID1",
      entry("QID1", "multipleChoiceSingle", { choicesFrom: "choices", noneChoiceKey: "c2" }),
      "role_not_for_type",
    ],
    [
      "fewer than two choices left",
      "QID1",
      entry("QID1", "multipleChoiceSingle", { choicesFrom: "choices", excludedKeys: ["c1", "c2"] }),
      "too_few_options",
    ],
    [
      "a rating range Formbricks has no scale for",
      "QID2",
      entry("QID2", "rating", { scale: "number", range: null }),
      "invalid_range",
    ],
    [
      "a CSAT that is not out of 5",
      "QID2",
      entry("QID2", "csat", { scale: "number", range: "7" }),
      "invalid_range",
    ],
    ["a CES out of 10", "QID2", entry("QID2", "ces", { scale: "number", range: "10" }), "invalid_range"],
    ["a date without a format", "QID3", entry("QID3", "date"), "missing_format"],
  ])("refuses %s", (_case, ref, planEntry, reason) => {
    expect(failureOf(checkQuestionRoles(question(simple, ref), planEntry))).toContain(reason);
  });

  test("checks a matrix's rows and columns come from two different lists", () => {
    const matrix = question(advanced, "QID1");

    expect(
      checkQuestionRoles(matrix, entry("QID1", "matrix", { rowsFrom: "choices", columnsFrom: "answers" })).ok
    ).toBe(true);
    expect(
      failureOf(
        checkQuestionRoles(matrix, entry("QID1", "matrix", { rowsFrom: "choices", columnsFrom: "choices" }))
      )
    ).toEqual(["missing_role"]);
  });

  test("settles a matrix's other list from the one the AI named", () => {
    const matrix = question(advanced, "QID1");
    const rowsOnly = checkQuestionRoles(matrix, entry("QID1", "matrix", { rowsFrom: "choices" }));
    const both = checkQuestionRoles(
      matrix,
      entry("QID1", "matrix", { rowsFrom: "choices", columnsFrom: "answers" })
    );

    expect(rowsOnly.ok && both.ok && rowsOnly.question.columns).toEqual(both.ok && both.question.columns);
    const columnsOnly = checkQuestionRoles(matrix, entry("QID1", "matrix", { columnsFrom: "answers" }));
    expect(columnsOnly.ok && both.ok && columnsOnly.question.rows).toEqual(both.ok && both.question.rows);
    expect(failureOf(checkQuestionRoles(matrix, entry("QID1", "matrix")))).toEqual(["missing_role"]);
  });

  test("caps a ranking at 25 options", () => {
    const ranking = question(advanced, "QID12");

    expect(
      failureOf(checkQuestionRoles(ranking, entry("QID12", "ranking", { choicesFrom: "choices" })))
    ).toEqual(["too_many_options"]);
    expect(
      checkQuestionRoles(
        ranking,
        entry("QID12", "ranking", { choicesFrom: "choices", excludedKeys: ["c41"] })
      ).ok
    ).toBe(true);
  });

  test("checks contact fields name the question's own choices, once each", () => {
    const form = question(advanced, "QID8");

    expect(
      checkQuestionRoles(
        form,
        entry("QID8", "contactInfo", { contactFields: [{ field: "firstName", key: "c11" }] })
      ).ok
    ).toBe(true);
    expect(
      failureOf(
        checkQuestionRoles(
          form,
          entry("QID8", "contactInfo", {
            contactFields: [
              { field: "firstName", key: "c11" },
              { field: "firstName", key: "c12" },
            ],
          })
        )
      )
    ).toEqual(["missing_role"]);
  });

  test("takes a rating with no scale as a number scale, and a range left out from its choices", () => {
    // QID2 has eleven choices, which only a CSAT's fixed five can stand in for.
    const csat = checkQuestionRoles(question(simple, "QID2"), entry("QID2", "csat", { scale: "number" }));
    const noScale = checkQuestionRoles(question(simple, "QID2"), entry("QID2", "rating", { range: "5" }));

    expect(csat.ok && csat.question.range).toBe(5);
    expect(noScale.ok).toBe(true);
  });

  test("checks an address form's fields are address fields", () => {
    const form = question(advanced, "QID8");

    expect(
      checkQuestionRoles(form, entry("QID8", "address", { contactFields: [{ field: "city", key: "c11" }] }))
        .ok
    ).toBe(true);
    expect(
      failureOf(
        checkQuestionRoles(
          form,
          entry("QID8", "address", { contactFields: [{ field: "email", key: "c11" }] })
        )
      )
    ).toEqual(["missing_role"]);
  });

  test("sizes a slider's rating from the slider, whatever range the model gave", () => {
    // QID4 is a five-star slider.
    const result = checkQuestionRoles(
      question(advanced, "QID4"),
      entry("QID4", "rating", { scale: "star", range: "7" })
    );

    expect(result.ok && result.question.range).toBe(5);
  });

  test("lists the options left out of a list the element shows, and only those", () => {
    const multipleChoice = checkQuestionRoles(
      question(simple, "QID1"),
      entry("QID1", "multipleChoiceSingle", { choicesFrom: "choices", excludedKeys: ["c2"] })
    );
    // An NPS question shows no choice list: its 0–10 choices are not a loss.
    const nps = checkQuestionRoles(question(simple, "QID2"), entry("QID2", "nps", { excludedKeys: ["c4"] }));

    expect(multipleChoice.ok && multipleChoice.question.leftOut).toEqual(["c2"]);
    expect(nps.ok && nps.question.leftOut).toEqual([]);
  });

  test("defaults an open text's input type and cleans its notes, one per rule", () => {
    const result = checkQuestionRoles(
      question(simple, "QID3"),
      entry("QID3", "openText", { logicNotes: ["  Shown if  'X' is\n'Y'. ", ""] })
    );

    expect(result.ok && result.question).toMatchObject({
      inputType: "text",
      notes: ["Shown if 'X' is 'Y'.", ""],
    });
  });
});

describe("sliderPoints", () => {
  test.each([
    [{ min: 0, max: 5, gridLines: null, stars: 5 }, 5],
    [{ min: 0, max: 7, gridLines: null, stars: null }, 8],
    [{ min: 1, max: 5, gridLines: 4, stars: null }, 5],
    [{ min: 0, max: 10, gridLines: 10, stars: null }, 11],
    [{ min: null, max: 7, gridLines: null, stars: null }, null],
    [{ min: 7, max: 0, gridLines: null, stars: null }, null],
  ])("counts %j as %s points", (slider, points) => {
    expect(sliderPoints(slider)).toBe(points);
  });
});

describe("ratingRangeFor", () => {
  test.each([
    [2, 3],
    [5, 5],
    [8, 10],
    [11, 10],
  ])("fits %s points into a %s-point rating", (points, range) => {
    expect(ratingRangeFor(points)).toBe(range);
  });
});

describe("checkPlanResponses", () => {
  const allRefs = new Set(["QID1", "QID2", "QID3", "QID4", "QID5"]);

  test("accepts the recorded plan whole", () => {
    const plan = checkPlanResponses(simple, [{ refs: allRefs, object: loadRecordedPlan("simple.qsf") }]);

    expect([...plan.questions.keys()]).toEqual(["QID1", "QID2", "QID3", "QID4", "QID5"]);
    expect(plan.failures.size).toBe(0);
    expect(plan.pageNotes.size).toBe(0);
  });

  test("drops, neutralizes or caps every part of a hostile plan", () => {
    const hostile = loadRecordedPlan("hostile");
    // The file spells its right-to-left override as an escape, so it holds no bidi character itself.
    expect(JSON.stringify(hostile)).toContain("\u202e");

    const plan = checkPlanResponses(simple, [{ refs: allRefs, object: hostile }]);

    expect(Object.fromEntries(plan.failures)).toEqual({
      QID1: ["placed_and_skipped"],
      QID2: ["type_not_allowed"],
      QID3: ["foreign_key"],
      QID4: ["duplicate_ref"],
      QID5: ["key_reused"],
    });
    expect(plan.questions.size).toBe(0);
    expect(plan.skipped.size).toBe(0);
    // Refs and pages outside the call — an unknown QID or page, `__proto__`, `constructor` — are
    // ignored. The call's own page keeps its notes, cleaned and capped.
    expect([...plan.pageNotes.keys()]).toEqual(["p1"]);
    expect(JSON.stringify([...plan.pageNotes.values()])).not.toMatch(/https?:|evil|<|javascript/);
    expect([...plan.pageNotes.values()].flat().join("")).not.toMatch(/[\u202a-\u202e\u2066-\u2069]/);
    expect(plan.pageNotes.get("p1")?.every((note) => note.length <= SURVEY_IMPORT_MAX_NOTE_CHARS)).toBe(true);
  });

  test("ignores refs another call was asked about", () => {
    const recorded = loadRecordedPlan("simple.qsf");

    const plan = checkPlanResponses(simple, [
      { refs: new Set(["QID1", "QID2"]), object: recorded },
      { refs: new Set(["QID3", "QID4", "QID5"]), object: { questions: [], skipped: [] } },
    ]);

    expect([...plan.questions.keys()]).toEqual(["QID1", "QID2"]);
    expect(Object.fromEntries(plan.failures)).toEqual({
      QID3: ["missing"],
      QID4: ["missing"],
      QID5: ["missing"],
    });
  });

  test("fails every question of a response it cannot read", () => {
    const plan = checkPlanResponses(simple, [{ refs: new Set(["QID1"]), object: "not a plan" }]);

    expect(Object.fromEntries(plan.failures)).toEqual({ QID1: ["invalid_output"] });
  });

  test("keeps page notes only for the pages the call holds questions of, first answer first", () => {
    const refs = new Set(["QID4"]);
    const recorded = loadRecordedPlan("logic-skip-display-branch.qsf");

    const plan = checkPlanResponses(logic, [
      {
        refs,
        object: {
          ...recorded,
          pages: [{ id: "p1", logicNotes: ["Not this call's page"] }, ...recorded.pages],
        },
      },
      { refs, object: { ...recorded, pages: [{ id: "p3", logicNotes: ["A later answer"] }] } },
    ]);

    expect(Object.fromEntries(plan.pageNotes)).toEqual({
      p3: ["Shown only to respondents who chose 'EU' for 'Which region are you in?'."],
    });
  });

  test("keeps a skip with its cleaned reason", () => {
    const plan = checkPlanResponses(simple, [
      {
        refs: new Set(["QID1"]),
        object: {
          questions: [],
          skipped: [{ ref: "QID1", reason: "Not supported, see https://x.y" }],
        },
      },
    ]);

    expect(Object.fromEntries(plan.skipped)).toEqual({ QID1: "Not supported, see …" });
  });
});

describe("mergeCheckedPlans", () => {
  test("a retry's results replace the first round's failures", () => {
    const first = checkPlanResponses(simple, [
      { refs: allRefsOf(simple), object: loadRecordedPlan("hostile") },
    ]);
    const retry = checkPlanResponses(simple, [
      { refs: new Set(first.failures.keys()), object: loadRecordedPlan("simple.qsf") },
    ]);

    const merged = mergeCheckedPlans(first, retry);

    expect(merged.failures.size).toBe(0);
    expect([...merged.questions.keys()].sort()).toEqual(["QID1", "QID2", "QID3", "QID4", "QID5"]);
    // The first round's page notes stay.
    expect([...merged.pageNotes.keys()]).toEqual(["p1"]);
  });
});

function allRefsOf(survey: typeof simple): Set<string> {
  return new Set(survey.questions.keys());
}
