import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/preact";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import type { TJsWorkspaceStateSurvey } from "@formbricks/types/js";
import { TSurveyElementTypeEnum } from "@formbricks/types/surveys/constants";
import { Survey } from "./survey";

const apiClientMocks = vi.hoisted(() => ({
  createDisplay: vi.fn(),
  createResponse: vi.fn(),
  updateResponse: vi.fn(),
  getResponseIdByDisplayId: vi.fn(),
}));

const offlineStorageMocks = vi.hoisted(() => ({
  addPendingResponse: vi.fn(),
  countPendingResponses: vi.fn(),
  getPendingResponses: vi.fn(),
  removePendingResponse: vi.fn(),
  clearSurveyProgress: vi.fn(),
  getSurveyProgress: vi.fn(),
  patchSurveyProgressSnapshot: vi.fn(),
  saveSurveyProgress: vi.fn(),
}));

vi.mock("@/lib/api-client", () => ({
  ApiClient: vi.fn(function ApiClient() {
    return apiClientMocks;
  }),
}));

vi.mock("@/lib/offline-storage", () => ({
  addPendingResponse: offlineStorageMocks.addPendingResponse,
  countPendingResponses: offlineStorageMocks.countPendingResponses,
  countPendingResponsesStrict: offlineStorageMocks.countPendingResponses,
  getPendingResponses: offlineStorageMocks.getPendingResponses,
  getPendingResponsesStrict: offlineStorageMocks.getPendingResponses,
  removePendingResponse: offlineStorageMocks.removePendingResponse,
  removePendingResponseStrict: offlineStorageMocks.removePendingResponse,
  clearSurveyProgress: offlineStorageMocks.clearSurveyProgress,
  getSurveyProgress: offlineStorageMocks.getSurveyProgress,
  patchSurveyProgressSnapshot: offlineStorageMocks.patchSurveyProgressSnapshot,
  saveSurveyProgress: offlineStorageMocks.saveSurveyProgress,
}));

vi.mock("@/lib/use-online-status", () => ({
  useOnlineStatus: () => true,
}));

vi.mock("@/lib/recall", () => ({
  parseRecallInformation: (element: unknown) => element,
}));

vi.mock("@/components/general/block-conditional", () => ({
  BlockConditional: ({ block, onSubmit, onBack, isFirstBlock }: any) => (
    <>
      <button
        data-testid={`submit-${block.id}`}
        onClick={() =>
          onSubmit(
            { [block.elements[0].id]: `${block.id}-answer` },
            {
              [block.elements[0].id]: 123,
            }
          )
        }>
        Submit {block.id}
      </button>
      {/* The real card hides Back on the first block it is told about. */}
      <button
        data-testid={`back-${block.id}`}
        data-first-block={String(!!isFirstBlock)}
        onClick={() => onBack()}>
        Back {block.id}
      </button>
    </>
  ),
}));

vi.mock("@/components/wrappers/stacked-cards-container", () => {
  // Mirrors the real container's index math: a `currentBlockId` that is not a block — an ending id,
  // the "end" sentinel, or a block deleted since progress was saved — maps past the end of the
  // array. The card before the current one is also rendered with live controls. The real container
  // makes that card inert (`pointerEvents: "none"`) and simple layouts do not mount it, so clicking
  // it here is a synthetic way to call the handlers from an off-block position, not a respondent
  // interaction.
  const resolveBlockIndex = (survey: any, currentBlockId: string): number => {
    if (currentBlockId === "start") return -1;
    const blockIndex = survey.blocks.findIndex((block: any) => block.id === currentBlockId);
    return blockIndex === -1 ? survey.blocks.length : blockIndex;
  };

  return {
    StackedCardsContainer: ({ currentBlockId, getCardContent, survey }: any) => {
      const blockIndex = resolveBlockIndex(survey, currentBlockId);
      return (
        <div data-testid="survey-root">
          {getCardContent(blockIndex, 0)}
          {blockIndex > 0 ? getCardContent(blockIndex - 1, 1) : null}
        </div>
      );
    },
  };
});

vi.mock("@/components/wrappers/auto-close-wrapper", () => ({
  AutoCloseWrapper: ({ children }: any) => <>{children}</>,
}));

vi.mock("@/components/general/ending-card", () => ({
  EndingCard: () => <div data-testid="ending-card">Ending</div>,
}));

vi.mock("@/components/general/welcome-card", () => ({
  WelcomeCard: () => <div data-testid="welcome-card">Welcome</div>,
}));

vi.mock("@/components/general/error-component", () => ({
  ErrorComponent: () => <div>Error</div>,
}));

vi.mock("@/components/general/response-error-component", () => ({
  ResponseErrorComponent: () => <div>Response Error</div>,
}));

vi.mock("@/components/general/formbricks-branding", () => ({
  FormbricksBranding: () => null,
}));

vi.mock("@/components/general/language-switch", () => ({
  LanguageSwitch: () => null,
}));

vi.mock("@/components/general/progress-bar", () => ({
  ProgressBar: () => null,
}));

vi.mock("@/components/general/recaptcha-branding", () => ({
  RecaptchaBranding: () => null,
}));

vi.mock("@/components/general/survey-close-button", () => ({
  SurveyCloseButton: () => null,
}));

const defaultLanguage = {
  default: true,
  enabled: true,
  language: {
    id: "lang123456789012345678901",
    code: "en",
    alias: null,
    createdAt: new Date(),
    updatedAt: new Date(),
    workspaceId: "workspace12345678901234567",
  },
};

const baseSurvey: TJsWorkspaceStateSurvey = {
  id: "survey12345678901234567890",
  name: "Offline Resume Survey",
  type: "link",
  status: "inProgress",
  questions: [],
  blocks: [
    {
      id: "block-1",
      elements: [
        {
          id: "q1",
          type: TSurveyElementTypeEnum.OpenText,
          required: false,
        },
      ],
      logic: [],
    },
    {
      id: "block-2",
      elements: [
        {
          id: "q2",
          type: TSurveyElementTypeEnum.OpenText,
          required: false,
        },
      ],
      logic: [],
    },
  ],
  endings: [{ id: "ending-1" }],
  welcomeCard: { enabled: false, timeToFinish: false, showResponseCount: false },
  variables: [],
  styling: { overwriteThemeStyling: false },
  recontactDays: null,
  displayLimit: null,
  displayPercentage: null,
  languages: [defaultLanguage],
  segment: null,
  hiddenFields: { enabled: false, fieldIds: [] },
  workspaceOverwrites: null,
  triggers: [],
  displayOption: "displayOnce",
  showLanguageSwitch: false,
  isBackButtonHidden: false,
  isAutoProgressingEnabled: false,
  recaptcha: {
    enabled: false,
  },
} as unknown as TJsWorkspaceStateSurvey;

const makeProgress = (overrides: Record<string, unknown> = {}) => ({
  surveyId: baseSurvey.id,
  blockId: "block-2",
  responseData: { q1: "saved-answer" },
  ttc: { q1: 111 },
  currentVariables: { savedVar: "saved" },
  history: ["block-1"],
  selectedLanguage: "en",
  surveyStateSnapshot: {
    responseId: null,
    displayId: "display1234567890123456789",
    surveyId: baseSurvey.id,
    singleUseId: null,
    userId: null,
    contactId: null,
    responseAcc: {
      finished: false,
      data: { q1: "saved-answer" },
      ttc: { q1: 111 },
      variables: { savedVar: "saved" },
    },
  },
  updatedAt: Date.now(),
  ...overrides,
});

const renderSurvey = () =>
  render(
    <Survey
      appUrl="http://localhost:3000"
      workspaceId="ws1234567890123456789012"
      survey={baseSurvey}
      styling={{} as any}
      isBrandingEnabled={false}
      languageCode="en"
      offlineSupport
      isSpamProtectionEnabled={false}
    />
  );

describe("Survey offline restore", () => {
  beforeEach(() => {
    vi.clearAllMocks();
    offlineStorageMocks.addPendingResponse.mockResolvedValue(1);
    offlineStorageMocks.countPendingResponses.mockResolvedValue(0);
    offlineStorageMocks.getPendingResponses.mockResolvedValue([]);
    offlineStorageMocks.removePendingResponse.mockResolvedValue(undefined);
    offlineStorageMocks.clearSurveyProgress.mockResolvedValue(undefined);
    offlineStorageMocks.patchSurveyProgressSnapshot.mockResolvedValue(undefined);
    offlineStorageMocks.saveSurveyProgress.mockResolvedValue(undefined);
    apiClientMocks.createDisplay.mockResolvedValue({ ok: true, data: { id: "display-created" } });
    apiClientMocks.createResponse.mockResolvedValue({ ok: true, data: { id: "response-created" } });
    apiClientMocks.updateResponse.mockResolvedValue({ ok: true, data: { quotaFull: false } });
    apiClientMocks.getResponseIdByDisplayId.mockResolvedValue({ ok: true, data: { responseId: null } });
    window.parent.postMessage = vi.fn();
  });

  afterEach(() => {
    cleanup();
  });

  test("recovers responseId from displayId and continues on the update path", async () => {
    offlineStorageMocks.getSurveyProgress.mockResolvedValue(makeProgress());
    apiClientMocks.getResponseIdByDisplayId.mockResolvedValue({
      ok: true,
      data: { responseId: "response-recovered" },
    });

    renderSurvey();

    await waitFor(() => {
      expect(apiClientMocks.getResponseIdByDisplayId).toHaveBeenCalledWith("display1234567890123456789");
    });

    fireEvent.click(await screen.findByTestId("submit-block-2"));

    await waitFor(() => {
      expect(apiClientMocks.updateResponse).toHaveBeenCalledWith(
        expect.objectContaining({
          responseId: "response-recovered",
          data: { q2: "block-2-answer" },
        })
      );
    });

    expect(apiClientMocks.createResponse).not.toHaveBeenCalled();
    expect(apiClientMocks.createDisplay).not.toHaveBeenCalled();
    expect(offlineStorageMocks.patchSurveyProgressSnapshot).toHaveBeenCalledWith(baseSurvey.id, {
      responseId: "response-recovered",
    });
  });

  test("bootstraps create from restored progress when display lookup returns no response", async () => {
    offlineStorageMocks.getSurveyProgress.mockResolvedValue(makeProgress());

    renderSurvey();

    fireEvent.click(await screen.findByTestId("submit-block-2"));

    await waitFor(() => {
      expect(apiClientMocks.createResponse).toHaveBeenCalledWith(
        expect.objectContaining({
          displayId: "display1234567890123456789",
          finished: true,
          data: { q1: "saved-answer", q2: "block-2-answer" },
          ttc: { q1: 111, q2: 123 },
          variables: { savedVar: "saved" },
        })
      );
    });

    expect(apiClientMocks.updateResponse).not.toHaveBeenCalled();
    expect(apiClientMocks.createDisplay).not.toHaveBeenCalled();
    expect(offlineStorageMocks.patchSurveyProgressSnapshot).toHaveBeenCalledWith(baseSurvey.id, {
      responseId: "response-created",
    });
  });

  test("creates a new display and bootstraps from restored progress when no ids are saved", async () => {
    offlineStorageMocks.getSurveyProgress.mockResolvedValue(
      makeProgress({
        surveyStateSnapshot: {
          responseId: null,
          displayId: null,
          surveyId: baseSurvey.id,
          singleUseId: null,
          userId: null,
          contactId: null,
          responseAcc: {
            finished: false,
            data: { q1: "saved-answer" },
            ttc: { q1: 111 },
            variables: { savedVar: "saved" },
          },
        },
      })
    );

    renderSurvey();

    await waitFor(() => {
      expect(apiClientMocks.createDisplay).toHaveBeenCalled();
    });

    fireEvent.click(await screen.findByTestId("submit-block-2"));

    await waitFor(() => {
      expect(apiClientMocks.createResponse).toHaveBeenCalledWith(
        expect.objectContaining({
          displayId: "display-created",
          data: { q1: "saved-answer", q2: "block-2-answer" },
        })
      );
    });

    expect(offlineStorageMocks.patchSurveyProgressSnapshot).toHaveBeenCalledWith(baseSurvey.id, {
      displayId: "display-created",
    });
  });

  test("sends the first ending id when the survey falls off the last block without a jump target", async () => {
    offlineStorageMocks.getSurveyProgress.mockResolvedValue(makeProgress());

    renderSurvey();

    fireEvent.click(await screen.findByTestId("submit-block-2"));

    await waitFor(() => {
      expect(apiClientMocks.createResponse).toHaveBeenCalledWith(
        expect.objectContaining({
          finished: true,
          endingId: "ending-1",
        })
      );
    });
  });

  test("skips responseId recovery while pending offline entries exist", async () => {
    offlineStorageMocks.getSurveyProgress.mockResolvedValue(makeProgress());
    offlineStorageMocks.countPendingResponses.mockResolvedValue(2);

    renderSurvey();

    await waitFor(() => {
      expect(offlineStorageMocks.countPendingResponses).toHaveBeenCalled();
    });

    expect(apiClientMocks.getResponseIdByDisplayId).not.toHaveBeenCalled();
  });

  test("a submit reaching the handler from the ending card is a no-op instead of throwing", async () => {
    offlineStorageMocks.getSurveyProgress.mockResolvedValue(makeProgress());

    renderSurvey();

    // Finish the survey, which moves the pointer onto the ending card.
    fireEvent.click(await screen.findByTestId("submit-block-2"));

    await waitFor(() => {
      expect(apiClientMocks.createResponse).toHaveBeenCalled();
    });

    expect(await screen.findByTestId("ending-card")).toBeTruthy();

    // Synthetic: the mock keeps the previous card clickable, so this calls `onSubmit` with the
    // pointer on the ending. It used to throw "Block not found" (ENG-2818); re-sending instead
    // would repeat `finished: true`, which the server rejects and which swaps the ending for the
    // error card. The response is already finished, so nothing is sent.
    fireEvent.click(await screen.findByTestId("submit-block-2"));

    await waitFor(() => {
      expect(screen.queryByTestId("ending-card")).toBeTruthy();
    });
    expect(apiClientMocks.updateResponse).not.toHaveBeenCalled();
  });

  test("back from the first block with no history is a no-op instead of throwing", async () => {
    offlineStorageMocks.getSurveyProgress.mockResolvedValue(
      makeProgress({ blockId: "block-1", history: [] })
    );

    renderSurvey();

    fireEvent.click(await screen.findByTestId("back-block-1"));

    // Still on the first block, and nothing was submitted — the previous behaviour threw
    // "Block not found" while reading blocks[-1] (ENG-2818).
    expect(await screen.findByTestId("submit-block-1")).toBeTruthy();
    expect(apiClientMocks.createResponse).not.toHaveBeenCalled();
    expect(apiClientMocks.updateResponse).not.toHaveBeenCalled();
  });

  test("clears stale finished progress instead of restoring the ending card", async () => {
    offlineStorageMocks.getSurveyProgress.mockResolvedValue(
      makeProgress({
        blockId: "ending-1",
        surveyStateSnapshot: {
          responseId: "response-finished",
          displayId: "display1234567890123456789",
          surveyId: baseSurvey.id,
          singleUseId: null,
          userId: null,
          contactId: null,
          responseAcc: {
            finished: true,
            data: { q1: "saved-answer", q2: "done" },
            ttc: { q1: 111, q2: 123 },
            variables: { savedVar: "saved" },
          },
        },
      })
    );

    renderSurvey();

    await waitFor(() => {
      expect(offlineStorageMocks.clearSurveyProgress).toHaveBeenCalledWith(baseSurvey.id);
    });

    expect(apiClientMocks.getResponseIdByDisplayId).not.toHaveBeenCalled();
  });
});

/**
 * The client wiring of the Flow tab's runtime (ENG-6932, ENG-6933): what `Survey` does with a rule
 * before the first question and a Mark as low quality rule, checked through the requests it sends.
 */
describe("Survey flow wiring", () => {
  /** `src` is not in the URL, so a rule on "src is not set" fires as the survey opens. */
  const startRule = (target: string) => ({
    id: "startrule000000000000001",
    conditions: {
      id: "startgroup00000000000001",
      connector: "and",
      conditions: [
        {
          id: "startcond000000000000001",
          leftOperand: { type: "hiddenField", value: "src" },
          operator: "isNotSet",
        },
      ],
    },
    actions: [{ id: "startaction0000000000001", objective: "jumpToBlock", target }],
  });

  const flowSurvey = (overrides: Record<string, unknown>) =>
    ({
      ...baseSurvey,
      hiddenFields: { enabled: true, fieldIds: ["src"] },
      ...overrides,
    }) as unknown as TJsWorkspaceStateSurvey;

  const renderFlowSurvey = (
    survey: TJsWorkspaceStateSurvey,
    props: { isSpamProtectionEnabled?: boolean; getRecaptchaToken?: () => Promise<string | null> } = {}
  ) =>
    render(
      <Survey
        appUrl="http://localhost:3000"
        workspaceId="ws1234567890123456789012"
        survey={survey}
        styling={{} as any}
        isBrandingEnabled={false}
        languageCode="en"
        offlineSupport
        isSpamProtectionEnabled={props.isSpamProtectionEnabled ?? false}
        getRecaptchaToken={props.getRecaptchaToken}
      />
    );

  beforeEach(() => {
    vi.clearAllMocks();
    offlineStorageMocks.addPendingResponse.mockResolvedValue(1);
    offlineStorageMocks.countPendingResponses.mockResolvedValue(0);
    offlineStorageMocks.getPendingResponses.mockResolvedValue([]);
    offlineStorageMocks.removePendingResponse.mockResolvedValue(undefined);
    offlineStorageMocks.clearSurveyProgress.mockResolvedValue(undefined);
    offlineStorageMocks.getSurveyProgress.mockResolvedValue(null);
    offlineStorageMocks.patchSurveyProgressSnapshot.mockResolvedValue(undefined);
    offlineStorageMocks.saveSurveyProgress.mockResolvedValue(undefined);
    apiClientMocks.createDisplay.mockResolvedValue({ ok: true, data: { id: "display-created" } });
    apiClientMocks.createResponse.mockResolvedValue({ ok: true, data: { id: "response-created" } });
    apiClientMocks.updateResponse.mockResolvedValue({ ok: true, data: { quotaFull: false } });
    apiClientMocks.getResponseIdByDisplayId.mockResolvedValue({ ok: true, data: { responseId: null } });
    window.parent.postMessage = vi.fn();
  });

  afterEach(() => {
    cleanup();
  });

  describe("Mark as low quality is sent once per response", () => {
    // block-2 marks every response that submits it Low quality.
    const markingSurvey = flowSurvey({
      blocks: [
        baseSurvey.blocks[0],
        {
          ...baseSurvey.blocks[1],
          logic: [
            {
              id: "lowqrule0000000000000001",
              conditions: { id: "lowqgroup000000000000001", connector: "and", conditions: [] },
              actions: [{ id: "lowqaction00000000000001", objective: "markLowQuality" }],
            },
          ],
        },
      ],
    });
    const sentMarkLowQuality = () =>
      [...apiClientMocks.createResponse.mock.calls, ...apiClientMocks.updateResponse.mock.calls].some(
        ([request]) => request?.markLowQuality === true
      );

    test("a session that already sent it, resumed from offline progress, does not send it again", async () => {
      offlineStorageMocks.getSurveyProgress.mockResolvedValue(makeProgress({ lowQualitySent: true }));

      renderFlowSurvey(markingSurvey);
      fireEvent.click(await screen.findByTestId("submit-block-2"));

      await waitFor(() => {
        expect(apiClientMocks.createResponse).toHaveBeenCalled();
      });
      expect(sentMarkLowQuality()).toBe(false);
    });

    test("a resumed session that never sent it sends it with the next write", async () => {
      offlineStorageMocks.getSurveyProgress.mockResolvedValue(makeProgress());

      renderFlowSurvey(markingSurvey);
      fireEvent.click(await screen.findByTestId("submit-block-2"));

      await waitFor(() => {
        expect(sentMarkLowQuality()).toBe(true);
      });
    });
  });

  test("a screen-out before the first question creates the finished response with a reCAPTCHA token", async () => {
    const getRecaptchaToken = vi.fn(async () => "recaptcha-token");

    renderFlowSurvey(
      flowSurvey({ startLogic: [startRule("ending-1")], recaptcha: { enabled: true, threshold: 0.5 } }),
      { isSpamProtectionEnabled: true, getRecaptchaToken }
    );

    await waitFor(() => {
      expect(apiClientMocks.createResponse).toHaveBeenCalledWith(
        expect.objectContaining({ finished: true, endingId: "ending-1", recaptchaToken: "recaptcha-token" })
      );
    });
    expect(getRecaptchaToken).toHaveBeenCalledTimes(1);
  });

  test("the block a start rule jumped to shows no Back: the respondent saw no page before it", async () => {
    renderFlowSurvey(flowSurvey({ startLogic: [startRule("block-2")] }));

    const back = await screen.findByTestId("back-block-2");

    await waitFor(() => {
      expect(back.getAttribute("data-first-block")).toBe("true");
    });
  });
});
