import { describe, expect, test, vi } from "vitest";
import { MAX_INGESTED_VALUE_BYTES } from "@formbricks/types/embedded-data-ingest";
import type { TLinkedEmbeddedField } from "@formbricks/types/embedded-data-resolver";
import type { TSurveyElement } from "@formbricks/types/surveys/elements";
import { buildAnswerPlan } from "./answers";
import {
  normalizeV3Ttc,
  planAnswerDataWrite,
  planEmbeddedDataWrite,
  resolveV3WriteLanguage,
  totalStoredV3Ttc,
  validateV3EndingId,
} from "./write-plan";

vi.mock("server-only", () => ({}));

const i18n = (value: string) => ({ default: value });

const element = (id: string): TSurveyElement =>
  ({
    id,
    type: "openText",
    headline: i18n(id),
    required: false,
    inputType: "text",
  }) as unknown as TSurveyElement;

/** A real plan from a real flatten — the write predicate is the plan's, so faking it proves nothing. */
const planFor = (elementIds: string[], ingestedStorageKeys: string[] = []) =>
  buildAnswerPlan(
    [{ id: "blk", name: "Block", elements: elementIds.map(element) }],
    "default",
    ingestedStorageKeys
  );

const declared = (
  name: string,
  source: "ingested" | "computed",
  over: { dataType?: string; locked?: boolean; storageKey?: string } = {}
): TLinkedEmbeddedField =>
  ({
    field: {
      name,
      source,
      dataType: over.dataType ?? "text",
      defaultValue: null,
      locked: over.locked ?? false,
    },
    link: { storageKey: over.storageKey ?? (source === "computed" ? "clvr000000000000000000001" : name) },
  }) as unknown as TLinkedEmbeddedField;

const planEmbedded = (
  fields: TLinkedEmbeddedField[],
  incoming: Record<string, string | number | boolean | null>,
  elementIds: string[] = []
) => planEmbeddedDataWrite({ embeddedFields: fields, elementIds: new Set(elementIds), incoming });

describe("planAnswerDataWrite — the write predicate is the read projection", () => {
  test("an element answer is written", () => {
    const { data, issues } = planAnswerDataWrite(planFor(["q1"]), { q1: "yes" }, undefined);

    expect(issues).toEqual([]);
    expect(data).toEqual({ q1: "yes" });
  });

  /**
   * The key that makes a read-edit-write round trip safe. `GET` withholds a hidden field's value, so
   * a caller sending back what it read carries no key for it — and a literal wholesale replace would
   * delete a value the caller never saw and could not have preserved.
   */
  /**
   * The reachable `__proto__` case, and the only one the API can produce.
   *
   * A caller cannot send the key — `z.record()` discards it before the body arrives — and a *publishable*
   * stored key is deliberately not carried, because the round trip expects the caller to resend it. What
   * is carried is a stored key the caller never sees: an ingested storage key, whose charset
   * (`isLegacyIdCharset`) admits `__proto__`, exactly as the note on `ingestedBag` says. Merging that onto
   * a plain `{}` runs `Object.prototype`'s setter instead of creating an own property, so a patch deletes
   * a stored value silently.
   *
   * Asserted through `Object.keys` and a JSON round trip rather than `toEqual`: an expected literal
   * written `{ __proto__: … }` swallows the key too, and the assertion would hold either way.
   */
  test("a stored ingested key named `__proto__` survives a patch rather than being dropped", () => {
    const { data } = planAnswerDataWrite(
      planFor(["q1"], ["__proto__"]),
      { q1: "new" },
      JSON.parse(String.raw`{"q1":"old","__proto__":"ingested"}`)
    );

    expect(Object.keys(data)).toContain("__proto__");
    expect(JSON.parse(JSON.stringify(data))["__proto__"]).toBe("ingested");
  });

  test("a stored hidden-field value survives a wholesale data replace", () => {
    const plan = planFor(["q1"], ["plan"]);

    const { data } = planAnswerDataWrite(plan, { q1: "new" }, { q1: "old", plan: "enterprise" });

    expect(data).toEqual({ q1: "new", plan: "enterprise" });
  });

  test("a system key stamped by the runtime survives the same replace", () => {
    const { data } = planAnswerDataWrite(
      planFor(["q1"]),
      { q1: "new" },
      { q1: "old", verifiedEmail: "a@b.test" }
    );

    expect(data.verifiedEmail).toBe("a@b.test");
  });

  /**
   * `GET` publishes a key but withholds its value when the stored bytes are outside the published
   * shapes (a legacy number array, a JSON `null`). A caller sending back what it read has no way to
   * resend that value, so omitting it must not delete it.
   */
  test("a stored answer whose value the read withholds survives a wholesale data replace", () => {
    const { data } = planAnswerDataWrite(
      planFor(["q1", "legacy", "blank"]),
      { q1: "new" },
      { q1: "old", legacy: [1, 2] as unknown as string[], blank: null as unknown as string }
    );

    expect(data).toEqual({ q1: "new", legacy: [1, 2], blank: null });
  });

  test("a withheld answer is replaced when the caller sends a new value for it", () => {
    const { data } = planAnswerDataWrite(
      planFor(["q1", "legacy"]),
      { q1: "new", legacy: ["a", "b"] },
      { q1: "old", legacy: [1, 2] as unknown as string[] }
    );

    expect(data).toEqual({ q1: "new", legacy: ["a", "b"] });
  });

  /** Replacement is still replacement for everything the caller *can* see. */
  test("an omitted answer is removed", () => {
    const { data } = planAnswerDataWrite(planFor(["q1", "q2"]), { q1: "kept" }, { q1: "old", q2: "dropped" });

    expect(data).toEqual({ q1: "kept" });
  });

  test("writing a hidden field through data is refused, and points at embeddedData", () => {
    const { issues } = planAnswerDataWrite(planFor(["q1"], ["plan"]), { plan: "free" }, undefined);

    expect(issues).toEqual([
      expect.objectContaining({ name: "plan", code: "unsupported_field", referenceType: "hiddenField" }),
    ]);
  });

  test("writing verifiedEmail is refused without claiming it is a hidden field", () => {
    const { issues } = planAnswerDataWrite(
      planFor(["q1"]),
      { verifiedEmail: "attacker@evil.test" },
      undefined
    );

    expect(issues).toHaveLength(1);
    expect(issues[0]).toMatchObject({ name: "verifiedEmail", code: "unsupported_field" });
    expect(issues[0].referenceType).toBeUndefined();
  });

  /**
   * An element id that also names a declared field: the answer owns the address, so this is the one
   * write that fits it. Refusing it would make the key unwritable by either route.
   */
  test("a key that is both an element id and a declared field name is accepted as an answer", () => {
    const { data, issues } = planAnswerDataWrite(
      planFor(["plan"], ["plan"]),
      { plan: "answered" },
      undefined
    );

    expect(issues).toEqual([]);
    expect(data).toEqual({ plan: "answered" });
  });

  /** A renamed or deleted element's value is still the caller's data, and the read hands it back. */
  test("an unknown key round-trips rather than being refused", () => {
    const { data, issues } = planAnswerDataWrite(planFor(["q1"]), { qOld: "legacy" }, undefined);

    expect(issues).toEqual([]);
    expect(data).toEqual({ qOld: "legacy" });
  });
});

describe("planEmbeddedDataWrite — names in, storage keys out", () => {
  test("a hidden field is resolved from its name to its storage key", () => {
    const fields = [declared("Plan", "ingested", { storageKey: "plan" })];

    expect(planEmbedded(fields, { Plan: "enterprise" }).dataWrites).toEqual({ plan: "enterprise" });
  });

  test("a variable is written to its cuid rather than into the answer map", () => {
    const fields = [declared("score", "computed", { dataType: "number", storageKey: "clvr0001" })];

    const plan = planEmbedded(fields, { score: 42 });

    expect(plan.variableWrites).toEqual({ clvr0001: 42 });
    expect(plan.dataWrites).toEqual({});
  });

  // ENG-6973: the shape Set value stores, whatever spelling the caller sent. A passed-in true/false
  // field keeps the text the ingest contract normalizes to.
  test("a true/false variable is stored as a real boolean, a hidden field as its text", () => {
    const fields = [
      declared("qualified", "computed", { dataType: "boolean", storageKey: "clvr0001" }),
      declared("is_pro", "ingested", { dataType: "boolean" }),
    ];

    expect(planEmbedded(fields, { qualified: true }).variableWrites).toEqual({ clvr0001: true });
    expect(planEmbedded(fields, { qualified: "false" }).variableWrites).toEqual({ clvr0001: false });
    expect(planEmbedded(fields, { qualified: "yes" }).variableWrites).toEqual({ clvr0001: true });
    expect(planEmbedded(fields, { is_pro: true }).dataWrites).toEqual({ is_pro: "true" });
    expect(planEmbedded(fields, { qualified: "maybe" }).issues).toEqual([
      expect.objectContaining({ name: "qualified", referenceType: "variable" }),
    ]);
  });

  /** Omission and `null` are the two halves of a merge; conflating them makes deletion unexpressible. */
  test("null clears and omission leaves alone", () => {
    const fields = [
      declared("plan", "ingested"),
      declared("tier", "ingested"),
      declared("score", "computed", { dataType: "number", storageKey: "clvr0001" }),
    ];

    const plan = planEmbedded(fields, { plan: null, score: null });

    expect(plan.dataClears).toEqual(["plan"]);
    expect(plan.variableClears).toEqual(["clvr0001"]);
    expect(plan.dataWrites).toEqual({});
    expect(plan.issues).toEqual([]);
  });

  test("a locked field ignores both a write and a clear", () => {
    const fields = [declared("plan", "ingested", { locked: true })];

    const written = planEmbedded(fields, { plan: "free" });
    const cleared = planEmbedded(fields, { plan: null });

    expect(written.dataWrites).toEqual({});
    expect(written.issues).toEqual([]);
    expect(cleared.dataClears).toEqual([]);
    expect(cleared.issues).toEqual([]);
  });

  test("an undeclared name is refused rather than silently dropped", () => {
    const { issues } = planEmbedded([declared("plan", "ingested")], { nope: "x" });

    expect(issues).toEqual([expect.objectContaining({ name: "nope", code: "unsupported_field" })]);
  });

  test("a reserved catalog name says where the three writable ones live", () => {
    const { issues } = planEmbedded([], { country: "PT" });

    expect(issues[0].reason).toContain("meta");
    expect(issues[0].reason).toContain("not writable");
  });

  /**
   * Two fields under one name cannot say which one to write, and guessing puts the caller's value in
   * the wrong field — silently, and only visible much later.
   */
  test("an ambiguous name is refused instead of guessed", () => {
    const fields = [declared("plan", "ingested"), declared("plan", "computed", { storageKey: "clvr0001" })];

    const plan = planEmbedded(fields, { plan: "x" });

    expect(plan.issues).toEqual([expect.objectContaining({ name: "plan", code: "duplicate_identifier" })]);
    expect(plan.dataWrites).toEqual({});
    expect(plan.variableWrites).toEqual({});
  });

  /**
   * The one that must never become a silent write: the ingest contract's own pass-through would
   * store this as the question's answer if the key reached it.
   */
  test("a field whose storage key is an element id cannot be written, and never lands on the answer", () => {
    const fields = [declared("plan", "ingested", { storageKey: "q1" })];

    const plan = planEmbedded(fields, { plan: "overwritten" }, ["q1"]);

    expect(plan.dataWrites).toEqual({});
    expect(plan.issues).toEqual([
      expect.objectContaining({ name: "plan", code: "unsupported_field", referenceType: "hiddenField" }),
    ]);
  });

  test("a variable that cannot hold the value is refused rather than corrupting a quota operand", () => {
    const fields = [declared("score", "computed", { dataType: "number", storageKey: "clvr0001" })];

    const plan = planEmbedded(fields, { score: "not a number" });

    expect(plan.variableWrites).toEqual({});
    expect(plan.issues).toEqual([expect.objectContaining({ name: "score", referenceType: "variable" })]);
  });

  /**
   * The ingested path truncates at this cap; this branch stored whatever arrived, so a text variable
   * could carry most of the 2 MB body limit into `Response.variables` — re-read into recall and quota
   * evaluation on every later read of the response.
   *
   * Refused rather than truncated, matching what this branch already does with a value that will not
   * coerce: a variable feeds calculations, so a silently cut value corrupts them.
   */
  test("a variable larger than the stored-value cap is refused, not stored", () => {
    const fields = [declared("notes", "computed", { dataType: "string", storageKey: "clvr0002" })];

    const plan = planEmbedded(fields, { notes: "a".repeat(MAX_INGESTED_VALUE_BYTES + 1) });

    expect(plan.variableWrites).toEqual({});
    expect(plan.issues).toEqual([
      expect.objectContaining({ name: "notes", code: "unsupported_field", referenceType: "variable" }),
    ]);
  });

  test("the cap is counted in UTF-8 bytes, not UTF-16 code units", () => {
    const fields = [declared("notes", "computed", { dataType: "string", storageKey: "clvr0002" })];

    // Each "あ" is 3 UTF-8 bytes, so this is under the cap by `length` and over it by bytes. Counting
    // code units would let a non-Latin value through at ~3x the documented limit.
    const overInBytes = "あ".repeat(Math.ceil(MAX_INGESTED_VALUE_BYTES / 3) + 1);
    expect(overInBytes.length).toBeLessThan(MAX_INGESTED_VALUE_BYTES);

    expect(planEmbedded(fields, { notes: overInBytes }).variableWrites).toEqual({});

    // And a value that fits is still stored, so the cap is not simply rejecting everything.
    const fits = "あ".repeat(10);
    expect(planEmbedded(fields, { notes: fits }).variableWrites).toEqual({ clvr0002: fits });
  });

  /**
   * Two payload names can resolve to one field, because the match is case-insensitive. Then one
   * entry's write and another's clear target the same slot and whichever ran last silently wins —
   * the same ambiguity as one name matching two fields, and refused the same way.
   */
  test("two payload names addressing one field are refused rather than raced", () => {
    const fields = [declared("Plan", "ingested", { storageKey: "plan" })];

    const plan = planEmbedded(fields, { Plan: "written", plan: null });

    expect(plan.issues).toEqual([expect.objectContaining({ name: "plan", code: "duplicate_identifier" })]);
    expect(plan.dataClears).toEqual([]);
    expect(plan.dataWrites).toEqual({ plan: "written" });
  });

  test("the same rule covers two writes to one field", () => {
    const fields = [declared("Plan", "ingested", { storageKey: "plan" })];

    expect(planEmbedded(fields, { Plan: "a", PLAN: "b" }).issues).toEqual([
      expect.objectContaining({ code: "duplicate_identifier" }),
    ]);
  });

  /**
   * A storage key only has to satisfy `isLegacyIdCharset`, which admits `__proto__`. On a plain `{}`
   * that key hits `Object.prototype`'s setter and vanishes with no value, no issue and no drop —
   * which is why the ingest contract itself uses a null-prototype object.
   */
  test("a __proto__ storage key is stored rather than swallowed", () => {
    const fields = [declared("Proto", "ingested", { storageKey: "__proto__" })];

    const plan = planEmbedded(fields, { Proto: "kept" });

    expect(plan.issues).toEqual([]);
    expect(Object.prototype.hasOwnProperty.call(plan.dataWrites, "__proto__")).toBe(true);
  });

  /** Hidden fields keep the SDK's lenient semantics: stored and flagged, never a 422. */
  test("a hidden field that fails coercion is still stored", () => {
    const fields = [declared("age", "ingested", { dataType: "number" })];

    const plan = planEmbedded(fields, { age: "not a number" });

    expect(plan.issues).toEqual([]);
    expect(plan.dataWrites.age).toBe("not a number");
  });
});

describe("resolveV3WriteLanguage", () => {
  const languages = [
    { default: true, enabled: true, language: { code: "en" } },
    { default: false, enabled: true, language: { code: "de" } },
    { default: false, enabled: false, language: { code: "fr" } },
  ];

  test.each([
    [null, null],
    [undefined, null],
    ["default", "default"],
  ])("%s resolves to %s", (input, expected) => {
    const resolved = resolveV3WriteLanguage(languages, input);
    expect(resolved).toEqual({ ok: true, code: expected });
  });

  /**
   * The defect this function exists for. `normalizeResponseLanguage` expands `de` to `de-DE`, which
   * no longer matches the survey's declared `de`, so the read falls back to the default and every
   * label on a German response comes back in English. Storing the survey's own code is what closes
   * that gap — validated value in, same value stored.
   */
  test("the survey's declared code is returned, not a canonicalized form of the caller's", () => {
    expect(resolveV3WriteLanguage(languages, "de")).toEqual({ ok: true, code: "de" });
  });

  test("a case variant still resolves to the survey's own casing", () => {
    expect(resolveV3WriteLanguage(languages, "DE")).toEqual({ ok: true, code: "de" });
  });

  /**
   * The realistic shape, and the one that caught a real divergence. A survey's language rows are
   * canonicalized when created, so a real survey declares `de-DE` while callers send `de`. v1 and v2
   * accept that because they canonicalize the caller's value; comparing raw strings made v3 answer
   * 422 for a payload the other two take. Matching is canonical; storing is still verbatim.
   */
  test("a bare code matches a survey that declares the canonical one, and stores the survey's", () => {
    const canonical = [
      { default: true, enabled: true, language: { code: "en-US" } },
      { default: false, enabled: true, language: { code: "de-DE" } },
    ];

    expect(resolveV3WriteLanguage(canonical, "de")).toEqual({ ok: true, code: "de-DE" });
    expect(resolveV3WriteLanguage(canonical, "de-DE")).toEqual({ ok: true, code: "de-DE" });
    expect(resolveV3WriteLanguage(canonical, "DE-de")).toEqual({ ok: true, code: "de-DE" });
  });

  /** And the reverse: a legacy survey declaring the bare code still takes a canonical payload. */
  test("a canonical code matches a survey that declares the bare one", () => {
    expect(resolveV3WriteLanguage(languages, "de-DE")).toEqual({ ok: true, code: "de" });
  });

  /** Canonical matching must not smuggle a language the survey never declared. */
  test("canonicalization does not widen the accepted set", () => {
    const canonical = [{ default: true, enabled: true, language: { code: "en-US" } }];

    expect(resolveV3WriteLanguage(canonical, "de").ok).toBe(false);
    expect(resolveV3WriteLanguage(canonical, "de-DE").ok).toBe(false);
  });

  test("a language the survey does not declare is refused", () => {
    const resolved = resolveV3WriteLanguage(languages, "es");
    expect(resolved.ok).toBe(false);
    expect(resolved.ok === false && resolved.issue).toMatchObject({
      name: "language",
      referenceType: "language",
    });
  });

  test("a disabled language is refused — enabled is exactly the question a new write asks", () => {
    expect(resolveV3WriteLanguage(languages, "fr").ok).toBe(false);
  });
});

describe("validateV3EndingId", () => {
  const endings = [{ id: "cmp1", type: "endScreen" }];

  test.each([null, undefined, "cmp1"])("%s is accepted", (endingId) => {
    expect(validateV3EndingId(endings, endingId)).toBeNull();
  });

  test("an ending from another survey is refused and names the id", () => {
    expect(validateV3EndingId(endings, "cmp-other")).toMatchObject({
      name: "endingId",
      referenceType: "ending",
      missingId: "cmp-other",
    });
  });
});

describe("normalizeV3Ttc", () => {
  /**
   * `_total` is derived, so a caller-supplied one would both contradict the sum of its own buckets
   * and be added into the total a second time by `calculateTtcTotal`.
   */
  test("a caller-supplied _total is dropped and recomputed on finish", () => {
    expect(normalizeV3Ttc({ q1: 1000, q2: 500, _total: 999_999 }, true)).toEqual({
      q1: 1000,
      q2: 500,
      _total: 1500,
    });
  });

  test("an unfinished response carries no total at all", () => {
    expect(normalizeV3Ttc({ q1: 1000 }, false)).toEqual({ q1: 1000 });
  });

  /** Client telemetry is clamped, never rejected: a laptop that slept mid-survey must not cost a response. */
  test.each([
    [-5, 0],
    [86_400_001, 86_400_000],
  ])("%d is clamped to %d", (input, expected) => {
    expect(normalizeV3Ttc({ q1: input }, false)).toEqual({ q1: expected });
  });

  /**
   * Every other write path stores `{}` for a missing map. Totalling an absent one publishes a
   * `durationSeconds` of zero, which reads as "answered instantly" rather than "not measured".
   */
  test("an omitted ttc stores {} even on a finished response", () => {
    expect(normalizeV3Ttc(undefined, true)).toEqual({});
    expect(normalizeV3Ttc(undefined, false)).toEqual({});
  });

  /** A supplied-but-empty map is still totalled, because that is what the other paths do with it. */
  test("a supplied empty ttc is totalled on finish", () => {
    expect(normalizeV3Ttc({}, true)).toEqual({ _total: 0 });
  });
});

describe("totalStoredV3Ttc", () => {
  /**
   * Stored buckets reach `clampTtcBuckets` without passing through Zod, so unlike a caller-supplied
   * `ttc` they can genuinely carry `__proto__` — and losing one silently changes a response's timing
   * total on the next write.
   */
  test("a stored `__proto__` bucket survives the read-back", () => {
    const total = totalStoredV3Ttc(JSON.parse(String.raw`{"q1":500,"__proto__":1200}`));

    expect(Object.keys(total)).toContain("__proto__");
    expect(JSON.parse(JSON.stringify(total))["__proto__"]).toBe(1200);
  });

  /**
   * The gap a patch leaves otherwise: `ttc` is create-only for a caller, but `_total` is derived, and
   * the shared update service computes it on any write that finishes a response. Without this a
   * response created partial and finished through PATCH reports no duration at all.
   */
  test("totals the stored buckets, ignoring any stored _total", () => {
    expect(totalStoredV3Ttc({ q1: 1000, nps: 500, _total: 999_999 })).toEqual({
      q1: 1000,
      nps: 500,
      _total: 1500,
    });
  });

  /**
   * `{}` is the common case, not an edge one: `Response.ttc` is `Json @default("{}")`, so every
   * response created without `ttc` stores a truthy empty object. This used to assert `{_total: 0}`,
   * which pinned the bug — `POST {finished:false}` with no `ttc` then `PATCH {finished:true}` reported
   * a `durationSeconds` of zero, i.e. "answered instantly" rather than "not measured".
   */
  test("a response with no stored timings stays unmeasured rather than totalling to zero", () => {
    expect(totalStoredV3Ttc(undefined)).toEqual({});
    expect(totalStoredV3Ttc({})).toEqual({});
    // Only `_total` and nothing to total: still no measurement.
    expect(totalStoredV3Ttc({ _total: 0 })).toEqual({});
    // Non-numeric buckets are dropped, so this is the same "nothing was measured" case.
    expect(totalStoredV3Ttc({ junk: "x" })).toEqual({});
  });

  test("non-numeric stored buckets are ignored rather than poisoning the sum", () => {
    expect(totalStoredV3Ttc({ q1: 1000, junk: "x" })).toEqual({ q1: 1000, _total: 1000 });
  });
});
