import { beforeEach, describe, expect, test, vi } from "vitest";
import { MAX_INGESTED_VALUE_BYTES } from "@formbricks/types/embedded-data-ingest";
import { POST } from "./route";

/**
 * Covers the ingest contract at the POST boundary **by crafting the request body**, which is the
 * only way to prove the AC's "server-side re-validation … that bypasses the client filter": the
 * renderer applies the same contract, so a test that goes through it proves nothing about what an
 * attacker posting straight at this endpoint can write.
 *
 * Everything around the contract is mocked; the contract itself, and `getElementsFromBlocks` under
 * it, run for real.
 */
const mocks = vi.hoisted(() => ({
  checkSurveyValidity: vi.fn(),
  createQuotaFullObject: vi.fn(),
  createResponseWithQuotaEvaluation: vi.fn(),
  findRecentDuplicateResponse: vi.fn(),
  formatValidationErrorsForV1Api: vi.fn((errors) => errors),
  getClientIpFromHeaders: vi.fn(),
  getIsContactsEnabled: vi.fn(),
  getOpenedByCreativeMeta: vi.fn(),
  getOrganizationIdFromWorkspaceId: vi.fn(),
  getSurvey: vi.fn(),
  reportApiError: vi.fn(),
  resolveClientApiIds: vi.fn(),
  sendToPipeline: vi.fn(),
  validateClientFileUploads: vi.fn(),
  validateOtherOptionLengthForMultipleChoice: vi.fn(),
  validateResponseData: vi.fn(),
}));

vi.mock("@/app/api/v2/client/[workspaceId]/responses/lib/utils", () => ({
  checkSurveyValidity: mocks.checkSurveyValidity,
}));

vi.mock("@/app/lib/api/api-error-reporter", () => ({
  reportApiError: mocks.reportApiError,
}));

vi.mock("@/app/lib/pipelines", () => ({
  sendToPipeline: mocks.sendToPipeline,
}));

vi.mock("@/lib/survey/service", () => ({
  getSurvey: mocks.getSurvey,
}));

vi.mock("@/lib/utils/client-ip", () => ({
  getClientIpFromHeaders: mocks.getClientIpFromHeaders,
}));

vi.mock("@/lib/utils/helper", () => ({
  getOrganizationIdFromWorkspaceId: mocks.getOrganizationIdFromWorkspaceId,
}));

vi.mock("@/lib/utils/resolve-client-id", () => ({
  resolveClientApiIds: mocks.resolveClientApiIds,
}));

vi.mock("@/modules/api/lib/validation", () => ({
  formatValidationErrorsForV1Api: mocks.formatValidationErrorsForV1Api,
  validateResponseData: mocks.validateResponseData,
}));

vi.mock("@/modules/api/v2/lib/element", () => ({
  validateOtherOptionLengthForMultipleChoice: mocks.validateOtherOptionLengthForMultipleChoice,
}));

vi.mock("@/modules/ee/creatives/lib/attribution", () => ({
  getOpenedByCreativeMeta: mocks.getOpenedByCreativeMeta,
}));

vi.mock("@/modules/ee/license-check/lib/utils", () => ({
  getIsContactsEnabled: mocks.getIsContactsEnabled,
}));

vi.mock("@/modules/ee/quotas/lib/helpers", () => ({
  createQuotaFullObject: mocks.createQuotaFullObject,
}));

vi.mock("@/modules/storage/utils", () => ({
  validateClientFileUploads: mocks.validateClientFileUploads,
}));

vi.mock("@/app/api/client/[workspaceId]/responses/lib/duplicate-response", () => ({
  findRecentDuplicateResponse: mocks.findRecentDuplicateResponse,
}));

vi.mock("./lib/response", () => ({
  createResponseWithQuotaEvaluation: mocks.createResponseWithQuotaEvaluation,
}));

const workspaceId = "lygo31gfsexlr4lh6rq8dxyl";
const surveyId = "cgt5e6dw1vsf1bv2ki5gj845";
const responseId = "c9471238d6c542b4bd1300e4";

const ingestedField = ({
  storageKey,
  dataType = "string",
  locked = false,
}: {
  storageKey: string;
  dataType?: string;
  locked?: boolean;
}) => ({
  field: { name: storageKey, source: "ingested", dataType, defaultValue: null, locked },
  link: { storageKey },
});

// `q1` is a real element, so an answer under that key is a question answer rather than an ingested value.
const getSurveyWithFields = (embeddedFields: unknown[]) => ({
  id: surveyId,
  workspaceId,
  status: "inProgress",
  blocks: [{ id: "block_1", elements: [{ id: "q1" }] }],
  questions: [],
  embeddedFields,
  isCaptureIpEnabled: false,
  isAnonymizeResponsesEnabled: false,
});

/** Posts a raw body straight at the endpoint, as a caller that never ran the renderer would. */
const postRawBody = async (
  data: Record<string, unknown>,
  extra: Record<string, unknown> = {}
): Promise<Response> => {
  const request = new Request(`https://api.test/api/v2/client/${workspaceId}/responses`, {
    method: "POST",
    headers: { "content-type": "application/json" },
    body: JSON.stringify({ surveyId, finished: false, data, ...extra }),
  });

  return POST(request, { params: Promise.resolve({ workspaceId }) });
};

/** What the route actually persisted, and the flags it computed for it. */
const persisted = () => {
  const [responseInput, ingestFlags] = mocks.createResponseWithQuotaEvaluation.mock.calls[0];
  return { data: responseInput.data, ingestFlags };
};

describe("POST /api/v2/client/[workspaceId]/responses — Embedded Data ingest contract", () => {
  beforeEach(() => {
    vi.clearAllMocks();

    mocks.resolveClientApiIds.mockResolvedValue({ workspaceId });
    mocks.getSurvey.mockResolvedValue(getSurveyWithFields([]));
    mocks.checkSurveyValidity.mockResolvedValue(null);
    mocks.validateClientFileUploads.mockReturnValue(true);
    mocks.validateOtherOptionLengthForMultipleChoice.mockReturnValue(null);
    mocks.validateResponseData.mockReturnValue(null);
    mocks.createQuotaFullObject.mockReturnValue({});
    mocks.findRecentDuplicateResponse.mockResolvedValue(null);
    mocks.createResponseWithQuotaEvaluation.mockResolvedValue({
      id: responseId,
      surveyId,
      finished: false,
      quotaFull: undefined,
    });
  });

  test("drops a key no ingested field declares", async () => {
    mocks.getSurvey.mockResolvedValue(getSurveyWithFields([ingestedField({ storageKey: "plan" })]));

    const response = await postRawBody({ q1: "answer", plan: "gold", rogue: "injected" });

    expect(response.status).toBe(200);
    expect(persisted().data).toEqual({ q1: "answer", plan: "gold" });
  });

  test("drops a locked field's key", async () => {
    mocks.getSurvey.mockResolvedValue(
      getSurveyWithFields([ingestedField({ storageKey: "plan", locked: true })])
    );

    const response = await postRawBody({ plan: "gold" });

    expect(response.status).toBe(200);
    expect(persisted().data).toEqual({});
  });

  test("truncates an oversize value and records the flag", async () => {
    mocks.getSurvey.mockResolvedValue(getSurveyWithFields([ingestedField({ storageKey: "note" })]));

    const response = await postRawBody({ note: "a".repeat(MAX_INGESTED_VALUE_BYTES + 500) });

    expect(response.status).toBe(200);
    expect(persisted().data.note as string).toHaveLength(MAX_INGESTED_VALUE_BYTES);
    expect(persisted().ingestFlags).toEqual([{ key: "note", reason: "truncated" }]);
  });

  test("stores a wrong-typed value raw, flags it, and still creates the response", async () => {
    mocks.getSurvey.mockResolvedValue(
      getSurveyWithFields([ingestedField({ storageKey: "seats", dataType: "number" })])
    );

    const response = await postRawBody({ seats: "many" });

    expect(response.status).toBe(200);
    expect(persisted().data).toEqual({ seats: "many" });
    expect(persisted().ingestFlags).toEqual([{ key: "seats", reason: "coercion_failed" }]);
  });

  test("coerces a declared value before validation and quota evaluation see it", async () => {
    mocks.getSurvey.mockResolvedValue(
      getSurveyWithFields([ingestedField({ storageKey: "seats", dataType: "number" })])
    );

    await postRawBody({ seats: "12" });

    expect(persisted().data).toEqual({ seats: 12 });
    // Both read the same object the contract replaced, so neither can act on the raw value.
    expect(mocks.validateResponseData).toHaveBeenCalledWith(
      expect.anything(),
      { seats: 12 },
      expect.anything(),
      expect.anything(),
      undefined
    );
    expect(mocks.checkSurveyValidity).toHaveBeenCalledWith(
      expect.anything(),
      workspaceId,
      expect.objectContaining({ data: { seats: 12 } })
    );
  });

  test("hands the not-shown record to validation, so rules skip what show-if kept back (ENG-7332)", async () => {
    const notShown = { rows: { trip: ["visa"] } };

    await postRawBody({}, { notShown });

    expect(mocks.validateResponseData).toHaveBeenCalledWith(
      expect.anything(),
      expect.anything(),
      expect.anything(),
      expect.anything(),
      notShown
    );
  });
});

describe("POST /api/v2/client/[workspaceId]/responses — duplicate submissions (ENG-1147)", () => {
  const contactId = "clh8ruz3w0000qa8h9x0bt9ry";
  const existingResponseId = "cm8f4x9mm0001gx9h5b7d7h3q";

  beforeEach(() => {
    vi.clearAllMocks();

    mocks.resolveClientApiIds.mockResolvedValue({ workspaceId });
    mocks.getOrganizationIdFromWorkspaceId.mockResolvedValue("org_1");
    mocks.getIsContactsEnabled.mockResolvedValue(true);
    mocks.getSurvey.mockResolvedValue({ ...getSurveyWithFields([]), type: "link" });
    mocks.checkSurveyValidity.mockResolvedValue(null);
    mocks.validateClientFileUploads.mockReturnValue(true);
    mocks.validateOtherOptionLengthForMultipleChoice.mockReturnValue(null);
    mocks.validateResponseData.mockReturnValue(null);
    mocks.createQuotaFullObject.mockReturnValue({ quotaFull: false });
    mocks.findRecentDuplicateResponse.mockResolvedValue({ id: existingResponseId });
  });

  test("hands back the earlier response's id instead of creating a second one", async () => {
    const response = await postRawBody({ q1: 5 }, { contactId, finished: true });

    expect(response.status).toBe(200);
    expect(await response.json()).toEqual({ data: { id: existingResponseId, quotaFull: false } });
    expect(mocks.findRecentDuplicateResponse).toHaveBeenCalledWith({
      surveyId,
      surveyType: "link",
      contactId,
      data: { q1: 5 },
      finished: true,
    });
    expect(mocks.createResponseWithQuotaEvaluation).not.toHaveBeenCalled();
    expect(mocks.sendToPipeline).not.toHaveBeenCalled();
  });
});

describe("POST /api/v2/client/[workspaceId]/responses — opened by creative (ENG-4001)", () => {
  const creativeId = "clh7m1qkx0000qwer1234abcd";

  const persistedMeta = () => mocks.createResponseWithQuotaEvaluation.mock.calls[0][0].meta;

  beforeEach(() => {
    vi.clearAllMocks();

    mocks.resolveClientApiIds.mockResolvedValue({ workspaceId });
    mocks.getSurvey.mockResolvedValue({ ...getSurveyWithFields([]), isAnonymizeResponsesEnabled: true });
    mocks.checkSurveyValidity.mockResolvedValue(null);
    mocks.validateClientFileUploads.mockReturnValue(true);
    mocks.validateOtherOptionLengthForMultipleChoice.mockReturnValue(null);
    mocks.validateResponseData.mockReturnValue(null);
    mocks.createQuotaFullObject.mockReturnValue({});
    mocks.findRecentDuplicateResponse.mockResolvedValue(null);
    mocks.createResponseWithQuotaEvaluation.mockResolvedValue({
      id: responseId,
      surveyId,
      finished: false,
      quotaFull: undefined,
    });
  });

  test("stamps the creative's own name, whatever name the client sent, and keeps it when anonymized", async () => {
    mocks.getOpenedByCreativeMeta.mockResolvedValue({
      openedByCreativeId: creativeId,
      openedByCreativeName: "Feedback tab",
    });

    const response = await postRawBody(
      { q1: "answer" },
      { meta: { openedByCreativeId: creativeId, openedByCreativeName: "Forged name" } }
    );

    expect(response.status).toBe(200);
    expect(mocks.getOpenedByCreativeMeta).toHaveBeenCalledWith({ creativeId, workspaceId, surveyId });
    expect(persistedMeta()).toEqual(
      expect.objectContaining({ openedByCreativeId: creativeId, openedByCreativeName: "Feedback tab" })
    );
  });

  test("stores no creative when it does not check out, and still creates the response", async () => {
    mocks.getOpenedByCreativeMeta.mockResolvedValue({});

    const response = await postRawBody(
      { q1: "answer" },
      { meta: { openedByCreativeId: creativeId, openedByCreativeName: "Forged name" } }
    );

    expect(response.status).toBe(200);
    expect(persistedMeta()).not.toHaveProperty("openedByCreativeId");
    expect(persistedMeta()).not.toHaveProperty("openedByCreativeName");
  });

  test("never takes a name from a client that names no creative", async () => {
    await postRawBody({ q1: "answer" }, { meta: { openedByCreativeName: "Forged name" } });

    expect(mocks.getOpenedByCreativeMeta).not.toHaveBeenCalled();
    expect(persistedMeta()).not.toHaveProperty("openedByCreativeName");
  });
});

describe("POST /api/v2/client/[workspaceId]/responses — Cal booking details", () => {
  const calSurvey = () => ({
    ...getSurveyWithFields([]),
    blocks: [
      {
        id: "block_1",
        elements: [{ id: "q1" }, { id: "cal1", type: "cal" }],
      },
    ],
  });
  const booking = { status: "booked", uid: "abc", startTime: "2026-12-01T10:00:00Z" };

  beforeEach(() => {
    vi.clearAllMocks();

    mocks.resolveClientApiIds.mockResolvedValue({ workspaceId });
    mocks.getSurvey.mockResolvedValue(calSurvey());
    mocks.checkSurveyValidity.mockResolvedValue(null);
    mocks.validateOtherOptionLengthForMultipleChoice.mockReturnValue(null);
    mocks.validateClientFileUploads.mockReturnValue(true);
    mocks.validateResponseData.mockReturnValue(null);
    mocks.createQuotaFullObject.mockReturnValue({});
    mocks.findRecentDuplicateResponse.mockResolvedValue(null);
    mocks.createResponseWithQuotaEvaluation.mockResolvedValue({
      id: responseId,
      surveyId,
      finished: false,
      quotaFull: undefined,
    });
  });

  const persistedInput = () => mocks.createResponseWithQuotaEvaluation.mock.calls[0][0];

  test('keeps the string "booked" in data and moves the details to calBookings', async () => {
    const result = await postRawBody({ q1: "answer", cal1: booking });

    expect(result.status).toBe(200);
    expect(persistedInput().data).toEqual({ q1: "answer", cal1: "booked" });
    expect(persistedInput().calBookings).toEqual({ cal1: booking });
  });

  test('leaves a legacy "booked" string alone and stores no details', async () => {
    await postRawBody({ cal1: "booked" });

    expect(persistedInput().data).toEqual({ cal1: "booked" });
    expect(persistedInput().calBookings).toBeUndefined();
  });

  test("answers 400 for a booking with a NUL byte, and stores nothing", async () => {
    const result = await postRawBody({ cal1: { ...booking, title: "Intro\u0000call" } });

    expect(result.status).toBe(400);
    expect(mocks.createResponseWithQuotaEvaluation).not.toHaveBeenCalled();
  });

  test("hands the booking to the duplicate lookup, which folds only a replay of the same one", async () => {
    await postRawBody({ cal1: booking }, { finished: true });

    expect(mocks.findRecentDuplicateResponse).toHaveBeenCalledWith(
      expect.objectContaining({ calBookings: { cal1: booking } })
    );
  });

  test("answers the survey check (PIN, workspace) before the booking error", async () => {
    mocks.checkSurveyValidity.mockResolvedValue(new Response(null, { status: 403 }));

    const result = await postRawBody({ cal1: { ...booking, title: "Intro\u0000call" } });

    expect(result.status).toBe(403);
  });

  test("ignores a calBookings key the client sends itself", async () => {
    await postRawBody({ cal1: "booked" }, { calBookings: { cal1: booking } });

    expect(persistedInput().calBookings).toBeUndefined();
  });
});
