import "@testing-library/jest-dom/vitest";
import { cleanup } from "@testing-library/react";
import { TFunction } from "i18next";
import { afterEach, describe, expect, test } from "vitest";
import { normalizeIngestedValue } from "@formbricks/types/embedded-data-ingest";
import { embeddedFieldsFromLegacyInput } from "@formbricks/types/embedded-data-mapping";
import { coerceToEmbeddedDataType } from "@formbricks/types/embedded-data-resolver";
import type { TSurveyQuota } from "@formbricks/types/quota";
import { type TSurveyElement, TSurveyElementTypeEnum } from "@formbricks/types/surveys/elements";
import { TSurvey, TSurveyLanguage } from "@formbricks/types/surveys/types";
import { TTag } from "@formbricks/types/tags";
import { TLanguage } from "@formbricks/types/workspace";
import {
  DateRange,
  SelectedFilterValue,
} from "@/app/(app)/workspaces/[workspaceId]/surveys/[surveyId]/(analysis)/components/response-filter-context";
import { OptionsType } from "@/app/(app)/workspaces/[workspaceId]/surveys/[surveyId]/components/response-filter-options";
import {
  buildDateFieldCondition,
  generateElementAndFilterOptions,
  getFormattedFilters,
  getTodayDate,
} from "./surveys";

const t = ((key: string) => key) as TFunction;

/** A survey as readers receive it: EmbeddedData rows inlined from the legacy columns (ENG-2412). */
const asRead = (survey: TSurvey): TSurvey => ({
  ...survey,
  embeddedFields: embeddedFieldsFromLegacyInput(survey),
});

const genOptions = (
  survey: TSurvey,
  extra: Partial<Parameters<typeof generateElementAndFilterOptions>[0]> = {}
) =>
  generateElementAndFilterOptions({
    survey: asRead(survey),
    environmentTags: undefined,
    attributes: {},
    reservedValues: {},
    hiddenFields: {},
    variableValues: {},
    quotas: [],
    t,
    ...extra,
  });

describe("surveys", () => {
  afterEach(() => {
    cleanup();
  });

  describe("generateQuestionAndFilterOptions", () => {
    test("should return question options for basic survey without additional options", () => {
      const survey = {
        id: "survey1",
        name: "Test Survey",
        blocks: [
          {
            id: "block1",
            name: "Block 1",
            elements: [
              {
                id: "q1",
                type: TSurveyElementTypeEnum.OpenText,
                headline: { default: "Open Text Question" },
                required: false,
                inputType: "text",
                charLimit: { enabled: false },
              } as TSurveyElement,
            ],
          },
        ],
        questions: [],
        createdAt: new Date(),
        updatedAt: new Date(),
        status: "draft",
      } as unknown as TSurvey;

      const result = genOptions(survey);

      expect(result.elementOptions.length).toBeGreaterThan(0);
      expect(result.elementOptions[0].header).toBe(OptionsType.ELEMENTS);
      const elementRows = result.elementFilterOptions.filter((o) => o.type !== "Meta");
      expect(elementRows).toHaveLength(1);
      expect(elementRows[0].id).toBe("q1");
    });

    test("should include tags in options when provided", () => {
      const survey = {
        id: "survey1",
        name: "Test Survey",
        blocks: [],
        questions: [],
        createdAt: new Date(),
        updatedAt: new Date(),
        status: "draft",
      } as unknown as TSurvey;

      const tags: TTag[] = [
        { id: "tag1", name: "Tag 1", workspaceId: "env1", createdAt: new Date(), updatedAt: new Date() },
      ];

      const result = genOptions(survey, { environmentTags: tags });

      const tagsHeader = result.elementOptions.find((opt) => opt.header === OptionsType.TAGS);
      expect(tagsHeader).toBeDefined();
      expect(tagsHeader?.option.length).toBe(1);
      expect(tagsHeader?.option[0].label).toBe("Tag 1");
    });

    test("should include attributes in options when provided", () => {
      const survey = {
        id: "survey1",
        name: "Test Survey",
        blocks: [],
        questions: [],
        createdAt: new Date(),
        updatedAt: new Date(),
        status: "draft",
      } as unknown as TSurvey;

      const attributes = {
        role: ["admin", "user"],
      };

      const result = genOptions(survey, { attributes });

      const attributesHeader = result.elementOptions.find((opt) => opt.header === OptionsType.ATTRIBUTES);
      expect(attributesHeader).toBeDefined();
      expect(attributesHeader?.option.length).toBe(1);
      expect(attributesHeader?.option[0].label).toBe("role");
    });

    test("meta options are catalog-driven, not derived from observed response values (ENG-1848)", () => {
      const survey = {
        id: "survey1",
        name: "Test Survey",
        blocks: [],
        questions: [],
        createdAt: new Date(),
        updatedAt: new Date(),
        status: "draft",
        isCaptureIpEnabled: true,
      } as unknown as TSurvey;

      // No observed values passed at all — the fields must still be offered.
      const result = genOptions(survey);

      const metaHeader = result.elementOptions.find((opt) => opt.header === OptionsType.META);
      expect(metaHeader).toBeDefined();
      const ids = metaHeader?.option.map((o) => o.id) ?? [];
      expect(ids).toContain("utmSource");
      expect(ids).toContain("browser");
      expect(ids).toContain("durationSeconds");
      // Covered elsewhere (response status, date range) or meaningless as user filters.
      for (const excluded of ["responseId", "surveyId", "finished", "startedAt", "language"]) {
        expect(ids).not.toContain(excluded);
      }

      // Observed values feed the combobox when provided.
      const withValues = genOptions(survey, { reservedValues: { utmSource: ["newsletter", "ads"] } });
      const utmSource = withValues.elementFilterOptions.find((o) => o.id === "utmSource");
      expect(utmSource?.filterComboBoxOptions).toEqual(["newsletter", "ads"]);
      expect(utmSource?.fieldDataType).toBe("string");
    });

    test("labels every meta option through `t()`, the filter-only durationSeconds included", () => {
      const survey = {
        id: "survey1",
        name: "Test Survey",
        blocks: [],
        questions: [],
        createdAt: new Date(),
        updatedAt: new Date(),
        status: "draft",
        isCaptureIpEnabled: true,
      } as unknown as TSurvey;

      const metaOptions =
        genOptions(survey).elementOptions.find((opt) => opt.header === OptionsType.META)?.option ?? [];
      expect(metaOptions.length).toBeGreaterThan(20);

      // `t` here is the identity, so each label IS the key it was routed through — a label derived from
      // the catalog name, such as `Duration Seconds`, carries no key and fails the pattern (ENG-2894).
      for (const { id, label } of metaOptions) {
        expect(label, id).toMatch(/^(workspace\.surveys\.responses|common)\.[a-z_]+$/);
      }
      expect(metaOptions.find((option) => option.id === "durationSeconds")?.label).toBe(
        "workspace.surveys.responses.duration_seconds"
      );
    });

    test("reserved options drop shadowed names and respect the IP capture toggle", () => {
      const survey = {
        id: "survey1",
        name: "Test Survey",
        blocks: [],
        questions: [],
        hiddenFields: { enabled: true, fieldIds: ["url"] },
        createdAt: new Date(),
        updatedAt: new Date(),
        status: "draft",
        isCaptureIpEnabled: false,
      } as unknown as TSurvey;

      const result = genOptions(survey);

      const metaIds =
        result.elementOptions.find((opt) => opt.header === OptionsType.META)?.option.map((o) => o.id) ?? [];
      expect(metaIds).not.toContain("url"); // the declared field owns the name
      expect(metaIds).not.toContain("ipAddress");
      expect(metaIds).toContain("pagePath");
    });

    test("hidden fields enumerate from the survey's declared ingested fields", () => {
      const survey = {
        id: "survey1",
        name: "Test Survey",
        blocks: [],
        questions: [],
        hiddenFields: { enabled: true, fieldIds: ["segment"] },
        createdAt: new Date(),
        updatedAt: new Date(),
        status: "draft",
      } as unknown as TSurvey;

      // A declared field is filterable even before observed values exist.
      const bare = genOptions(survey);
      const bareHeader = bare.elementOptions.find((opt) => opt.header === OptionsType.HIDDEN_FIELDS);
      expect(bareHeader?.option.length).toBe(1);
      expect(bareHeader?.option[0].label).toBe("segment");

      const result = genOptions(survey, { hiddenFields: { segment: ["free", "paid"] } });
      const segment = result.elementFilterOptions.find(
        (o) => o.type === "Hidden Fields" && o.id === "segment"
      );
      expect(segment?.filterComboBoxOptions).toEqual(["free", "paid"]);
      expect(segment?.filterOptions).toContain("Contains");
      expect(segment?.filterOptions).toContain("Is set");
    });

    test("variables enumerate from the survey's computed fields, keyed by storageKey", () => {
      const survey = {
        id: "survey1",
        name: "Test Survey",
        blocks: [],
        questions: [],
        variables: [{ id: "var_score", name: "score", type: "number", value: 0 }],
        createdAt: new Date(),
        updatedAt: new Date(),
        status: "draft",
      } as unknown as TSurvey;

      const result = genOptions(survey);

      const variablesHeader = result.elementOptions.find((opt) => opt.header === OptionsType.VARIABLES);
      expect(variablesHeader?.option).toEqual([
        { label: "score", type: OptionsType.VARIABLES, id: "var_score" },
      ]);
      const score = result.elementFilterOptions.find((o) => o.type === "Variables" && o.id === "var_score");
      expect(score?.fieldDataType).toBe("number");
      expect(score?.filterOptions).toEqual([
        "Equals",
        "Not equals",
        "Is greater than",
        "Is less than",
        "Is set",
        "Is not set",
      ]);
    });

    test("a calculated true/false field filters by True or False (ENG-6973)", () => {
      const survey = {
        id: "survey1",
        name: "Test Survey",
        blocks: [],
        questions: [],
        embeddedFields: [
          {
            field: {
              key: null,
              name: "qualified",
              source: "computed",
              dataType: "boolean",
              defaultValue: null,
              locked: false,
            },
            link: { storageKey: "var_qualified" },
          },
        ],
        createdAt: new Date(),
        updatedAt: new Date(),
        status: "draft",
      } as unknown as TSurvey;

      // Not through `asRead`: a true/false field is declared only in the typed rows.
      const result = generateElementAndFilterOptions({
        survey,
        environmentTags: undefined,
        attributes: {},
        reservedValues: {},
        hiddenFields: {},
        variableValues: {},
        quotas: [],
        t,
      });

      const variablesHeader = result.elementOptions.find((opt) => opt.header === OptionsType.VARIABLES);
      expect(variablesHeader?.option).toEqual([
        { label: "qualified", type: OptionsType.VARIABLES, id: "var_qualified" },
      ]);
      const qualified = result.elementFilterOptions.find(
        (o) => o.type === "Variables" && o.id === "var_qualified"
      );
      expect(qualified?.filterOptions).toEqual(["Equals", "Not equals", "Is set", "Is not set"]);
      expect(qualified?.filterComboBoxOptions).toEqual(["true", "false"]);
    });

    test("lists Hidden Fields and Variables above Meta in the filter picker", () => {
      const survey = {
        id: "survey1",
        name: "Test Survey",
        blocks: [],
        questions: [],
        hiddenFields: { enabled: true, fieldIds: ["plan"] },
        variables: [{ id: "var_score", name: "score", type: "number", value: 0 }],
        createdAt: new Date(),
        updatedAt: new Date(),
        status: "draft",
      } as unknown as TSurvey;

      const headers = genOptions(survey).elementOptions.map((opt) => opt.header);

      expect(headers.indexOf(OptionsType.HIDDEN_FIELDS)).toBeGreaterThan(-1);
      expect(headers.indexOf(OptionsType.VARIABLES)).toBeGreaterThan(-1);
      expect(headers.indexOf(OptionsType.HIDDEN_FIELDS)).toBeLessThan(headers.indexOf(OptionsType.META));
      expect(headers.indexOf(OptionsType.VARIABLES)).toBeLessThan(headers.indexOf(OptionsType.META));
    });

    test("should include language options when survey has languages", () => {
      const survey = {
        id: "survey1",
        name: "Test Survey",
        blocks: [],
        questions: [],
        createdAt: new Date(),
        updatedAt: new Date(),
        status: "draft",
        languages: [{ language: { code: "en" } as unknown as TLanguage } as unknown as TSurveyLanguage],
      } as unknown as TSurvey;

      const result = genOptions(survey);

      const othersHeader = result.elementOptions.find((opt) => opt.header === OptionsType.OTHERS);
      expect(othersHeader).toBeDefined();
      expect(othersHeader?.option.some((o) => o.label === "Language")).toBeTruthy();
    });

    test("should handle all question types correctly", () => {
      const survey = {
        id: "survey1",
        name: "Test Survey",
        blocks: [
          {
            id: "block1",
            name: "Block 1",
            elements: [
              {
                id: "q1",
                type: TSurveyElementTypeEnum.OpenText,
                headline: { default: "Open Text" },
                required: false,
                inputType: "text",
                charLimit: { enabled: false },
              },
              {
                id: "q2",
                type: TSurveyElementTypeEnum.MultipleChoiceSingle,
                headline: { default: "Multiple Choice Single" },
                required: false,
                choices: [{ id: "c1", label: { default: "Choice 1" } }],
                shuffleOption: "none",
              },
              {
                id: "q3",
                type: TSurveyElementTypeEnum.MultipleChoiceMulti,
                headline: { default: "Multiple Choice Multi" },
                required: false,
                choices: [
                  { id: "c1", label: { default: "Choice 1" } },
                  { id: "other", label: { default: "Other" } },
                ],
                shuffleOption: "none",
              },
              {
                id: "q4",
                type: TSurveyElementTypeEnum.NPS,
                headline: { default: "NPS" },
                required: false,
                lowerLabel: { default: "Not likely" },
                upperLabel: { default: "Very likely" },
              },
              {
                id: "q5",
                type: TSurveyElementTypeEnum.Rating,
                headline: { default: "Rating" },
                required: false,
                scale: "number",
                range: 5,
                lowerLabel: { default: "Low" },
                upperLabel: { default: "High" },
              },
              {
                id: "q6",
                type: TSurveyElementTypeEnum.CTA,
                headline: { default: "CTA" },
                required: false,
                buttonLabel: { default: "Click me" },
                buttonExternal: false,
              },
              {
                id: "q7",
                type: TSurveyElementTypeEnum.PictureSelection,
                headline: { default: "Picture Selection" },
                required: false,
                allowMultiple: false,
                choices: [
                  { id: "p1", imageUrl: "url1" },
                  { id: "p2", imageUrl: "url2" },
                ],
              },
              {
                id: "q8",
                type: TSurveyElementTypeEnum.Matrix,
                headline: { default: "Matrix" },
                required: false,
                rows: [{ id: "r1", label: { default: "Row 1" } }],
                columns: [{ id: "c1", label: { default: "Column 1" } }],
              },
            ] as TSurveyElement[],
          },
        ],
        questions: [],
        createdAt: new Date(),
        updatedAt: new Date(),
        status: "draft",
      } as unknown as TSurvey;

      const result = genOptions(survey);

      expect(result.elementFilterOptions.filter((o) => o.type !== "Meta")).toHaveLength(8);
      expect(result.elementFilterOptions.some((o) => o.id === "q1")).toBeTruthy();
      expect(result.elementFilterOptions.some((o) => o.id === "q2")).toBeTruthy();
      expect(result.elementFilterOptions.some((o) => o.id === "q7")).toBeTruthy();
      expect(result.elementFilterOptions.some((o) => o.id === "q8")).toBeTruthy();
    });

    test("reserved fields get operators per dataType (ENG-1848)", () => {
      const survey = {
        id: "survey1",
        name: "Test Survey",
        blocks: [],
        questions: [],
        createdAt: new Date(),
        updatedAt: new Date(),
        status: "draft",
      } as unknown as TSurvey;

      const result = genOptions(survey);

      const urlFilterOption = result.elementFilterOptions.find((o) => o.type === "Meta" && o.id === "url");
      expect(urlFilterOption?.filterOptions).toEqual([
        "Equals",
        "Not equals",
        "Contains",
        "Does not contain",
        "Starts with",
        "Does not start with",
        "Ends with",
        "Does not end with",
        "Is set",
        "Is not set",
      ]);

      const screenWidthOption = result.elementFilterOptions.find(
        (o) => o.type === "Meta" && o.id === "screenWidth"
      );
      expect(screenWidthOption?.fieldDataType).toBe("number");
      expect(screenWidthOption?.filterOptions).toEqual([
        "Equals",
        "Not equals",
        "Is greater than",
        "Is less than",
        "Is set",
        "Is not set",
      ]);
    });

    test("should include quota options in filter options when quotas are provided", () => {
      const survey = {
        id: "survey1",
        name: "Test Survey",
        blocks: [],
        createdAt: new Date(),
        updatedAt: new Date(),
        status: "draft",
      } as unknown as TSurvey;

      const quotas = [{ id: "quota1" }];

      const result = genOptions(survey, { quotas: quotas as TSurveyQuota[] });

      const quotaFilterOption = result.elementFilterOptions.find((o) => o.id === "quota1");
      expect(quotaFilterOption).toBeDefined();
      expect(quotaFilterOption?.type).toBe("Quotas");
      expect(quotaFilterOption?.filterOptions).toEqual(["Status"]);
      expect(quotaFilterOption?.filterComboBoxOptions).toEqual([
        "Screened in",
        "Screened out (overquota)",
        "Not in quota",
      ]);
    });

    test("should include multiple quota options when multiple quotas are provided", () => {
      const survey = {
        id: "survey1",
        name: "Test Survey",
        blocks: [],
        createdAt: new Date(),
        updatedAt: new Date(),
        status: "draft",
      } as unknown as TSurvey;

      const quotas = [{ id: "quota1" }, { id: "quota2" }];

      const result = genOptions(survey, { quotas: quotas as TSurveyQuota[] });

      const quota1 = result.elementFilterOptions.find((o) => o.id === "quota1");
      const quota2 = result.elementFilterOptions.find((o) => o.id === "quota2");

      expect(quota1).toBeDefined();
      expect(quota2).toBeDefined();
      expect(quota1?.filterComboBoxOptions).toEqual([
        "Screened in",
        "Screened out (overquota)",
        "Not in quota",
      ]);
      expect(quota2?.filterComboBoxOptions).toEqual([
        "Screened in",
        "Screened out (overquota)",
        "Not in quota",
      ]);
    });
  });

  describe("getFormattedFilters", () => {
    const survey = asRead({
      id: "survey1",
      name: "Test Survey",
      blocks: [
        {
          id: "block1",
          name: "Block 1",
          elements: [
            {
              id: "openTextQ",
              type: TSurveyElementTypeEnum.OpenText,
              headline: { default: "Open Text" },
              required: false,
              inputType: "text",
              charLimit: { enabled: false },
            },
            {
              id: "mcSingleQ",
              type: TSurveyElementTypeEnum.MultipleChoiceSingle,
              headline: { default: "Multiple Choice Single" },
              required: false,
              choices: [{ id: "c1", label: { default: "Choice 1" } }],
              shuffleOption: "none",
            },
            {
              id: "mcMultiQ",
              type: TSurveyElementTypeEnum.MultipleChoiceMulti,
              headline: { default: "Multiple Choice Multi" },
              required: false,
              choices: [{ id: "c1", label: { default: "Choice 1" } }],
              shuffleOption: "none",
            },
            {
              id: "npsQ",
              type: TSurveyElementTypeEnum.NPS,
              headline: { default: "NPS" },
              required: false,
              lowerLabel: { default: "Not likely" },
              upperLabel: { default: "Very likely" },
            },
            {
              id: "ratingQ",
              type: TSurveyElementTypeEnum.Rating,
              headline: { default: "Rating" },
              required: false,
              scale: "number",
              range: 5,
              lowerLabel: { default: "Low" },
              upperLabel: { default: "High" },
            },
            {
              id: "ctaQ",
              type: TSurveyElementTypeEnum.CTA,
              headline: { default: "CTA" },
              required: false,
              buttonLabel: { default: "Click me" },
              buttonExternal: false,
            },
            {
              id: "consentQ",
              type: TSurveyElementTypeEnum.Consent,
              headline: { default: "Consent" },
              required: false,
              label: { default: "I agree" },
            },
            {
              id: "pictureQ",
              type: TSurveyElementTypeEnum.PictureSelection,
              headline: { default: "Picture Selection" },
              required: false,
              allowMultiple: false,
              choices: [
                { id: "p1", imageUrl: "url1" },
                { id: "p2", imageUrl: "url2" },
              ],
            },
            {
              id: "matrixQ",
              type: TSurveyElementTypeEnum.Matrix,
              headline: { default: "Matrix" },
              required: false,
              rows: [{ id: "r1", label: { default: "Row 1" } }],
              columns: [{ id: "c1", label: { default: "Column 1" } }],
            },
            {
              id: "addressQ",
              type: TSurveyElementTypeEnum.Address,
              headline: { default: "Address" },
              required: false,
              zip: { show: true, required: false, placeholder: { default: "Zip" } },
              city: { show: true, required: false, placeholder: { default: "City" } },
              state: { show: true, required: false, placeholder: { default: "State" } },
              country: { show: true, required: false, placeholder: { default: "Country" } },
              addressLine1: { show: true, required: false, placeholder: { default: "Address Line 1" } },
              addressLine2: { show: true, required: false, placeholder: { default: "Address Line 2" } },
            },
            {
              id: "contactQ",
              type: TSurveyElementTypeEnum.ContactInfo,
              headline: { default: "Contact Info" },
              required: false,
              firstName: { show: true, required: false, placeholder: { default: "First Name" } },
              lastName: { show: true, required: false, placeholder: { default: "Last Name" } },
              email: { show: true, required: false, placeholder: { default: "Email" } },
              phone: { show: true, required: false, placeholder: { default: "Phone" } },
              company: { show: true, required: false, placeholder: { default: "Company" } },
            },
            {
              id: "rankingQ",
              type: TSurveyElementTypeEnum.Ranking,
              headline: { default: "Ranking" },
              required: false,
              choices: [{ id: "r1", label: { default: "Option 1" } }],
            },
          ] as TSurveyElement[],
        },
      ],
      questions: [],
      hiddenFields: { enabled: true, fieldIds: ["plan"] },
      variables: [{ id: "var_score", name: "score", type: "number", value: 0 }],
      createdAt: new Date(),
      updatedAt: new Date(),
      status: "draft",
    } as unknown as TSurvey);

    const dateRange: DateRange = {
      from: new Date("2023-01-01"),
      to: new Date("2023-01-31"),
    };

    test("should return empty filters when no selections", () => {
      const selectedFilter: SelectedFilterValue = {
        responseStatuses: [],
        filter: [],
      };

      const result = getFormattedFilters(
        survey,
        selectedFilter,
        {} as Parameters<typeof getFormattedFilters>[2]
      );

      expect(Object.keys(result)).toHaveLength(0);
    });

    test("should filter by completed responses", () => {
      const selectedFilter: SelectedFilterValue = {
        responseStatuses: ["complete"],
        filter: [],
      };

      const result = getFormattedFilters(survey, selectedFilter, { from: undefined });

      expect(result.finished).toBe(true);
    });

    test("should filter by the Low quality outcome without touching finished", () => {
      const selectedFilter: SelectedFilterValue = {
        responseStatuses: ["lowQuality"],
        filter: [],
      };

      const result = getFormattedFilters(
        survey,
        selectedFilter,
        {} as Parameters<typeof getFormattedFilters>[2]
      );

      expect(result.outcome).toBe("lowQuality");
      expect(result.finished).toBeUndefined();
    });

    test("should filter by date range", () => {
      const selectedFilter: SelectedFilterValue = {
        responseStatuses: [],
        filter: [],
      };

      const result = getFormattedFilters(survey, selectedFilter, dateRange);

      expect(result.createdAt).toBeDefined();
      expect(result.createdAt?.min).toEqual(dateRange.from);
      expect(result.createdAt?.max).toEqual(dateRange.to);
    });

    test("should filter by tags", () => {
      const selectedFilter: SelectedFilterValue = {
        responseStatuses: [],
        filter: [
          {
            elementType: { type: "Tags", label: "Tag 1", id: "tag1" },
            filterType: { filterValue: undefined, filterComboBoxValue: "Applied" },
          },
          {
            elementType: { type: "Tags", label: "Tag 2", id: "tag2" },
            filterType: { filterValue: undefined, filterComboBoxValue: "Not applied" },
          },
        ] as SelectedFilterValue["filter"],
      };

      const result = getFormattedFilters(
        survey,
        selectedFilter,
        {} as Parameters<typeof getFormattedFilters>[2]
      );

      expect(result.tags?.applied).toContain("Tag 1");
      expect(result.tags?.notApplied).toContain("Tag 2");
    });

    test("should filter by open text questions", () => {
      const selectedFilter: SelectedFilterValue = {
        responseStatuses: [],
        filter: [
          {
            elementType: {
              type: "Elements",
              label: "Open Text",
              id: "openTextQ",
              elementType: TSurveyElementTypeEnum.OpenText,
            },
            filterType: { filterValue: undefined, filterComboBoxValue: "Filled out" },
          },
        ],
      } as SelectedFilterValue;

      const result = getFormattedFilters(
        survey,
        selectedFilter,
        {} as Parameters<typeof getFormattedFilters>[2]
      );

      expect(result.data?.openTextQ).toEqual({ op: "filledOut" });
    });

    test("should filter by address questions", () => {
      const selectedFilter: SelectedFilterValue = {
        responseStatuses: [],
        filter: [
          {
            elementType: {
              type: "Elements",
              label: "Address",
              id: "addressQ",
              elementType: TSurveyElementTypeEnum.Address,
            },
            filterType: { filterValue: undefined, filterComboBoxValue: "Skipped" },
          },
        ],
      } as SelectedFilterValue;

      const result = getFormattedFilters(
        survey,
        selectedFilter,
        {} as Parameters<typeof getFormattedFilters>[2]
      );

      expect(result.data?.addressQ).toEqual({ op: "skipped" });
    });

    test("should filter by contact info questions", () => {
      const selectedFilter: SelectedFilterValue = {
        responseStatuses: [],
        filter: [
          {
            elementType: {
              type: "Elements",
              label: "Contact Info",
              id: "contactQ",
              elementType: TSurveyElementTypeEnum.ContactInfo,
            },
            filterType: { filterValue: undefined, filterComboBoxValue: "Filled out" },
          },
        ],
      } as SelectedFilterValue;

      const result = getFormattedFilters(
        survey,
        selectedFilter,
        {} as Parameters<typeof getFormattedFilters>[2]
      );

      expect(result.data?.contactQ).toEqual({ op: "filledOut" });
    });

    test("should filter by ranking questions", () => {
      const selectedFilter: SelectedFilterValue = {
        responseStatuses: [],
        filter: [
          {
            elementType: {
              type: "Elements",
              label: "Ranking",
              id: "rankingQ",
              elementType: TSurveyElementTypeEnum.Ranking,
            },
            filterType: { filterValue: undefined, filterComboBoxValue: "Filled out" },
          },
        ],
      } as SelectedFilterValue;

      const result = getFormattedFilters(
        survey,
        selectedFilter,
        {} as Parameters<typeof getFormattedFilters>[2]
      );

      expect(result.data?.rankingQ).toEqual({ op: "submitted" });
    });

    test("should filter by multiple choice single questions", () => {
      const selectedFilter: SelectedFilterValue = {
        responseStatuses: [],
        filter: [
          {
            elementType: {
              type: "Elements",
              label: "MC Single",
              id: "mcSingleQ",
              elementType: TSurveyElementTypeEnum.MultipleChoiceSingle,
            },
            filterType: { filterValue: "Includes either", filterComboBoxValue: ["Choice 1"] },
          },
        ],
      } as SelectedFilterValue;

      const result = getFormattedFilters(
        survey,
        selectedFilter,
        {} as Parameters<typeof getFormattedFilters>[2]
      );

      expect(result.data?.mcSingleQ).toEqual({ op: "includesOne", value: ["Choice 1"] });
    });

    test("should filter by multiple choice multi questions", () => {
      const selectedFilter: SelectedFilterValue = {
        responseStatuses: [],
        filter: [
          {
            elementType: {
              type: "Elements",
              label: "MC Multi",
              id: "mcMultiQ",
              elementType: TSurveyElementTypeEnum.MultipleChoiceMulti,
            },
            filterType: { filterValue: "Includes all", filterComboBoxValue: ["Choice 1", "Choice 2"] },
          },
        ],
      } as SelectedFilterValue;

      const result = getFormattedFilters(
        survey,
        selectedFilter,
        {} as Parameters<typeof getFormattedFilters>[2]
      );

      expect(result.data?.mcMultiQ).toEqual({ op: "includesAll", value: ["Choice 1", "Choice 2"] });
    });

    test("should filter by NPS questions with different operations", () => {
      const selectedFilter: SelectedFilterValue = {
        responseStatuses: [],
        filter: [
          {
            elementType: {
              type: "Elements",
              label: "NPS",
              id: "npsQ",
              elementType: TSurveyElementTypeEnum.NPS,
            },
            filterType: { filterValue: "Is equal to", filterComboBoxValue: "7" },
          },
        ],
      } as SelectedFilterValue;

      const result = getFormattedFilters(
        survey,
        selectedFilter,
        {} as Parameters<typeof getFormattedFilters>[2]
      );

      expect(result.data?.npsQ).toEqual({ op: "equals", value: 7 });
    });

    test("should filter by rating questions with less than operation", () => {
      const selectedFilter: SelectedFilterValue = {
        responseStatuses: [],
        filter: [
          {
            elementType: {
              type: "Elements",
              label: "Rating",
              id: "ratingQ",
              elementType: TSurveyElementTypeEnum.Rating,
            },
            filterType: { filterValue: "Is less than", filterComboBoxValue: "4" },
          },
        ],
      } as SelectedFilterValue;

      const result = getFormattedFilters(
        survey,
        selectedFilter,
        {} as Parameters<typeof getFormattedFilters>[2]
      );

      expect(result.data?.ratingQ).toEqual({ op: "lessThan", value: 4 });
    });

    test("should filter by CTA questions", () => {
      const selectedFilter: SelectedFilterValue = {
        responseStatuses: [],
        filter: [
          {
            elementType: {
              type: "Elements",
              label: "CTA",
              id: "ctaQ",
              elementType: TSurveyElementTypeEnum.CTA,
            },
            filterType: { filterValue: undefined, filterComboBoxValue: "Clicked" },
          },
        ],
      } as SelectedFilterValue;

      const result = getFormattedFilters(
        survey,
        selectedFilter,
        {} as Parameters<typeof getFormattedFilters>[2]
      );

      expect(result.data?.ctaQ).toEqual({ op: "clicked" });
    });

    test("should filter by consent questions", () => {
      const selectedFilter: SelectedFilterValue = {
        responseStatuses: [],
        filter: [
          {
            elementType: {
              type: "Elements",
              label: "Consent",
              id: "consentQ",
              elementType: TSurveyElementTypeEnum.Consent,
            },
            filterType: { filterValue: undefined, filterComboBoxValue: "Accepted" },
          },
        ],
      } as SelectedFilterValue;

      const result = getFormattedFilters(
        survey,
        selectedFilter,
        {} as Parameters<typeof getFormattedFilters>[2]
      );

      expect(result.data?.consentQ).toEqual({ op: "accepted" });
    });

    test("should filter by picture selection questions", () => {
      const selectedFilter: SelectedFilterValue = {
        responseStatuses: [],
        filter: [
          {
            elementType: {
              type: "Elements",
              label: "Picture",
              id: "pictureQ",
              elementType: TSurveyElementTypeEnum.PictureSelection,
            },
            filterType: { filterValue: "Includes either", filterComboBoxValue: ["Picture 1"] },
          },
        ],
      } as SelectedFilterValue;

      const result = getFormattedFilters(
        survey,
        selectedFilter,
        {} as Parameters<typeof getFormattedFilters>[2]
      );

      expect(result.data?.pictureQ).toEqual({ op: "includesOne", value: ["p1"] });
    });

    test("should filter by matrix questions", () => {
      const selectedFilter: SelectedFilterValue = {
        responseStatuses: [],
        filter: [
          {
            elementType: {
              type: "Elements",
              label: "Matrix",
              id: "matrixQ",
              elementType: TSurveyElementTypeEnum.Matrix,
            },
            filterType: { filterValue: "Row 1", filterComboBoxValue: "Column 1" },
          },
        ],
      } as SelectedFilterValue;

      const result = getFormattedFilters(
        survey,
        selectedFilter,
        {} as Parameters<typeof getFormattedFilters>[2]
      );

      expect(result.data?.matrixQ).toEqual({ op: "matrix", value: { "Row 1": "Column 1" } });
    });

    test("rules on two rows of one matrix share its condition", () => {
      const row = (filterValue: string, filterComboBoxValue: string) => ({
        elementType: {
          type: "Elements",
          label: "Matrix",
          id: "matrixQ",
          elementType: TSurveyElementTypeEnum.Matrix,
        },
        filterType: { filterValue, filterComboBoxValue },
      });
      const result = getFormattedFilters(
        survey,
        {
          responseStatuses: [],
          filter: [row("Row 1", "Column 1"), row("Row 2", "Column 2")],
        } as SelectedFilterValue,
        {} as DateRange
      );

      expect(result.data?.matrixQ).toEqual({
        op: "matrix",
        value: { "Row 1": "Column 1", "Row 2": "Column 2" },
      });
    });

    test("should filter by hidden fields", () => {
      const selectedFilter: SelectedFilterValue = {
        responseStatuses: [],
        filter: [
          {
            elementType: { type: "Hidden Fields", label: "plan", id: "plan" },
            filterType: { filterValue: "Equals", filterComboBoxValue: "pro" },
          },
        ],
      } as SelectedFilterValue;

      const result = getFormattedFilters(
        survey,
        selectedFilter,
        {} as Parameters<typeof getFormattedFilters>[2]
      );

      expect(result.data?.plan).toEqual({ op: "equals", value: "pro" });
    });

    test("should filter by attributes", () => {
      const selectedFilter: SelectedFilterValue = {
        responseStatuses: [],
        filter: [
          {
            elementType: { type: "Attributes", label: "role", id: "role" },
            filterType: { filterValue: "Not equals", filterComboBoxValue: "admin" },
          },
        ],
      } as SelectedFilterValue;

      const result = getFormattedFilters(
        survey,
        selectedFilter,
        {} as Parameters<typeof getFormattedFilters>[2]
      );

      expect(result.contactAttributes?.role).toEqual({ op: "notEquals", value: "admin" });
    });

    test("should filter by other filters", () => {
      const selectedFilter: SelectedFilterValue = {
        responseStatuses: [],
        filter: [
          {
            elementType: { type: "Other Filters", label: "Language", id: "language" },
            filterType: { filterValue: "Equals", filterComboBoxValue: "en" },
          },
        ],
      } as SelectedFilterValue;

      const result = getFormattedFilters(
        survey,
        selectedFilter,
        {} as Parameters<typeof getFormattedFilters>[2]
      );

      expect(result.others?.Language).toEqual({ op: "equals", value: "en" });
    });

    test("reserved fields land in the reserved group, keyed by catalog name (ENG-1848)", () => {
      const selectedFilter: SelectedFilterValue = {
        responseStatuses: [],
        filter: [
          {
            elementType: { type: "Meta", label: "Source", id: "source" },
            filterType: { filterValue: "Not equals", filterComboBoxValue: "web" },
          },
        ],
      } as SelectedFilterValue;

      const result = getFormattedFilters(
        survey,
        selectedFilter,
        {} as Parameters<typeof getFormattedFilters>[2]
      );

      expect(result.reserved?.source).toEqual({ op: "notEquals", value: "web" });
      expect(result.meta).toBeUndefined();
    });

    test("number-typed reserved values are sent as numbers, presence ops carry no value", () => {
      const selectedFilter: SelectedFilterValue = {
        responseStatuses: [],
        filter: [
          {
            elementType: { type: "Meta", label: "Screen Width", id: "screenWidth" },
            filterType: { filterValue: "Is greater than", filterComboBoxValue: "1000" },
          },
          {
            elementType: { type: "Meta", label: "UTM Source", id: "utmSource" },
            filterType: { filterValue: "Is set", filterComboBoxValue: undefined },
          },
        ],
      } as SelectedFilterValue;

      const result = getFormattedFilters(
        survey,
        selectedFilter,
        {} as Parameters<typeof getFormattedFilters>[2]
      );

      expect(result.reserved?.screenWidth).toEqual({ op: "greaterThan", value: 1000 });
      expect(result.reserved?.utmSource).toEqual({ op: "isSet" });
    });

    test("fails closed: an unknown or shadowed reserved id emits nothing", () => {
      const selectedFilter: SelectedFilterValue = {
        responseStatuses: [],
        filter: [
          {
            elementType: { type: "Meta", label: "Nope", id: "notInCatalog" },
            filterType: { filterValue: "Equals", filterComboBoxValue: "x" },
          },
        ],
      } as SelectedFilterValue;

      const result = getFormattedFilters(
        survey,
        selectedFilter,
        {} as Parameters<typeof getFormattedFilters>[2]
      );

      expect(result.reserved).toBeUndefined();
    });

    test("variables land in the variables group keyed by storageKey, coerced to their dataType", () => {
      const selectedFilter: SelectedFilterValue = {
        responseStatuses: [],
        filter: [
          {
            elementType: { type: "Variables", label: "score", id: "var_score" },
            filterType: { filterValue: "Is less than", filterComboBoxValue: "5" },
          },
        ],
      } as SelectedFilterValue;

      const result = getFormattedFilters(
        survey,
        selectedFilter,
        {} as Parameters<typeof getFormattedFilters>[2]
      );

      expect(result.variables?.var_score).toEqual({ op: "lessThan", value: 5 });
    });

    // Legacy-derived fields are all string-typed, so craft the typed rows directly.
    const typedSurvey = {
      ...survey,
      embeddedFields: [
        ...(survey.embeddedFields ?? []),
        {
          field: {
            name: "is_pro",
            source: "ingested",
            dataType: "boolean",
            defaultValue: null,
            locked: false,
          },
          link: { storageKey: "is_pro" },
        },
        {
          field: {
            name: "signup_date",
            source: "ingested",
            dataType: "date",
            defaultValue: null,
            locked: false,
          },
          link: { storageKey: "signup_date" },
        },
      ],
    } as TSurvey;

    const boolFilter = (filterValue: string, filterComboBoxValue: string) => {
      const selectedFilter: SelectedFilterValue = {
        responseStatuses: [],
        filter: [
          {
            elementType: { type: OptionsType.HIDDEN_FIELDS, label: "is_pro", id: "is_pro" },
            filterType: { filterValue, filterComboBoxValue },
          },
        ],
      };

      return getFormattedFilters(typedSurvey, selectedFilter, { from: undefined });
    };

    test("boolean and date ingested fields coerce per dataType; invalid numbers drop the row", () => {
      const result = getFormattedFilters(
        typedSurvey,
        {
          responseStatuses: [],
          filter: [
            {
              elementType: { type: "Hidden Fields", label: "is_pro", id: "is_pro" },
              filterType: { filterValue: "Equals", filterComboBoxValue: "true" },
            },
            {
              elementType: { type: "Hidden Fields", label: "signup_date", id: "signup_date" },
              filterType: { filterValue: "Is before", filterComboBoxValue: "2026-01-01" },
            },
            {
              elementType: { type: "Variables", label: "score", id: "var_score" },
              filterType: { filterValue: "Is less than", filterComboBoxValue: "abc" },
            },
          ],
        } as Parameters<typeof getFormattedFilters>[1],
        {} as Parameters<typeof getFormattedFilters>[2]
      );

      // Booleans are stored as the strings the ingest contract writes, so the filter value is one
      // too — a jsonb boolean here matched no row (ENG-3231).
      expect(result.data?.is_pro).toEqual({ op: "equals", value: "true" });
      // Dates ride the comparison ops with the ISO string.
      expect(result.data?.signup_date).toEqual({ op: "lessThan", value: "2026-01-01" });
      // A non-numeric value for a number field cannot form a condition — the row drops.
      expect(result.variables).toBeUndefined();

      // A text op makes no sense on a boolean — dropped rather than matching wrongly.
      expect(boolFilter("Contains", "tru").data?.is_pro).toBeUndefined();

      // Only the exact literals are booleans — "yes" must drop, not coerce to false.
      expect(boolFilter("Equals", "yes").data?.is_pro).toBeUndefined();
    });

    test("a boolean condition carries the same spelling the ingest contract stores", () => {
      // The bug this pins (ENG-3231): the filter sent a real jsonb boolean while every write path
      // stores the string, so `is_pro equals true` matched nothing the moment a boolean field held a
      // value. Asserting against `normalizeIngestedValue` rather than a hand-written "true" is what
      // keeps the two seams from drifting apart again — whatever the ingest contract decides the
      // canonical spelling is, this is the value the filter has to compare against.
      for (const [label, incoming] of [
        ["true", "1"],
        ["false", "off"],
      ] as const) {
        const stored = normalizeIngestedValue(incoming, "boolean");
        expect(stored).toEqual({ value: label });

        expect(boolFilter("Equals", label).data?.is_pro).toEqual({ op: "equals", value: stored?.value });
        expect(boolFilter("Not equals", label).data?.is_pro).toEqual({
          op: "notEquals",
          value: stored?.value,
        });
      }
    });

    test("the picker offers a boolean field exactly the spellings the condition builder accepts", () => {
      // The other half of the contract above. `buildTypedFieldCondition` takes only these two
      // literals and drops the row for anything else, so the picker is what guarantees the value
      // arrives in its stored form. Change this list — to `Yes`/`No`, or back to real booleans —
      // and every boolean filter row silently stops reaching the query instead of failing loudly.
      //
      // Called directly rather than through `genOptions`, whose `asRead` replaces `embeddedFields`
      // with the legacy-derived rows — and those are all string-typed, so the boolean branch under
      // test would never run.
      const { elementFilterOptions } = generateElementAndFilterOptions({
        survey: typedSurvey,
        environmentTags: undefined,
        attributes: {},
        reservedValues: {},
        hiddenFields: {},
        variableValues: {},
        quotas: [],
        t,
      });

      expect(elementFilterOptions.find((option) => option.id === "is_pro")?.filterComboBoxOptions).toEqual([
        "true",
        "false",
      ]);
    });

    test("a date row reaches the data group as the window its value names", () => {
      // The bug this pins (ENG-3232): the row's value is day-granular — the picker is an
      // `<input type="date">` — while the column stores whatever arrived, so `equals` compared a day
      // against an instant and missed it, and `Is after` matched instants on the day it excluded.
      const dateFilter = (filterValue: string, filterComboBoxValue: string) =>
        getFormattedFilters(
          typedSurvey,
          {
            responseStatuses: [],
            filter: [
              {
                elementType: {
                  type: OptionsType.HIDDEN_FIELDS,
                  label: "signup_date",
                  id: "signup_date",
                },
                filterType: { filterValue, filterComboBoxValue },
              },
            ],
          },
          { from: undefined }
        ).data?.signup_date;

      const min = "2026-09-01";
      const max = "2026-09-02";

      expect(dateFilter("Equals", min)).toEqual({ op: "inRange", min, max });
      expect(dateFilter("Not equals", min)).toEqual({ op: "notInRange", min, max });
      expect(dateFilter("Is after", min)).toEqual({ op: "greaterEqual", value: max });
      expect(dateFilter("Is before", min)).toEqual({ op: "lessThan", value: min });
      // Presence still answers on the data group's own vocabulary, window or not.
      expect(dateFilter("Is set", "")).toEqual({ op: "submitted" });
    });

    test("ingested presence maps onto the data group's submitted/skipped vocabulary", () => {
      const selectedFilter: SelectedFilterValue = {
        responseStatuses: [],
        filter: [
          {
            elementType: { type: "Hidden Fields", label: "plan", id: "plan" },
            filterType: { filterValue: "Is not set", filterComboBoxValue: undefined },
          },
        ],
      } as SelectedFilterValue;

      const result = getFormattedFilters(
        survey,
        selectedFilter,
        {} as Parameters<typeof getFormattedFilters>[2]
      );

      expect(result.data?.plan).toEqual({ op: "skipped" });
    });

    test("should handle multiple filters together", () => {
      const selectedFilter: SelectedFilterValue = {
        responseStatuses: ["complete"],
        filter: [
          {
            elementType: {
              type: "Elements",
              label: "NPS",
              id: "npsQ",
              elementType: TSurveyElementTypeEnum.NPS,
            },
            filterType: { filterValue: "Is more than", filterComboBoxValue: "7" },
          },
          {
            elementType: { type: "Tags", label: "Tag 1", id: "tag1" },
            filterType: { filterValue: undefined, filterComboBoxValue: "Applied" },
          },
        ],
      } as SelectedFilterValue;

      const result = getFormattedFilters(survey, selectedFilter, dateRange);

      expect(result.finished).toBe(true);
      expect(result.createdAt).toBeDefined();
      expect(result.data?.npsQ).toEqual({ op: "greaterThan", value: 7 });
      expect(result.tags?.applied).toContain("Tag 1");
    });

    test("should format URL meta filters with string operations", () => {
      const selectedFilter: SelectedFilterValue = {
        responseStatuses: [],
        filter: [
          {
            elementType: { type: "Meta", label: "url", id: "url" },
            filterType: { filterValue: "Contains", filterComboBoxValue: "example.com" },
          },
        ],
      } as SelectedFilterValue;

      const result = getFormattedFilters(survey, selectedFilter, dateRange);

      expect(result.reserved?.url).toEqual({ op: "contains", value: "example.com" });
    });

    test("should format URL meta filters with all supported string operations", () => {
      const testCases = [
        { filterValue: "Equals", expected: { op: "equals", value: "https://example.com" } },
        { filterValue: "Not equals", expected: { op: "notEquals", value: "https://example.com" } },
        { filterValue: "Contains", expected: { op: "contains", value: "example.com" } },
        { filterValue: "Does not contain", expected: { op: "doesNotContain", value: "test.com" } },
        { filterValue: "Starts with", expected: { op: "startsWith", value: "https://" } },
        { filterValue: "Does not start with", expected: { op: "doesNotStartWith", value: "http://" } },
        { filterValue: "Ends with", expected: { op: "endsWith", value: ".com" } },
        { filterValue: "Does not end with", expected: { op: "doesNotEndWith", value: ".org" } },
      ];

      testCases.forEach(({ filterValue, expected }) => {
        const selectedFilter: SelectedFilterValue = {
          responseStatuses: [],
          filter: [
            {
              elementType: { type: "Meta", label: "url", id: "url" },
              filterType: { filterValue, filterComboBoxValue: expected.value },
            },
          ],
        } as SelectedFilterValue;

        const result = getFormattedFilters(survey, selectedFilter, dateRange);
        expect(result.reserved?.url).toEqual(expected);
      });
    });

    test("should handle URL meta filters with empty string values", () => {
      const selectedFilter: SelectedFilterValue = {
        responseStatuses: [],
        filter: [
          {
            elementType: { type: "Meta", label: "url", id: "url" },
            filterType: { filterValue: "Contains", filterComboBoxValue: "" },
          },
        ],
      } as SelectedFilterValue;

      const result = getFormattedFilters(survey, selectedFilter, dateRange);

      expect(result.reserved?.url).toBeUndefined();
    });

    test("should handle URL meta filters with whitespace-only values", () => {
      const selectedFilter: SelectedFilterValue = {
        responseStatuses: [],
        filter: [
          {
            elementType: { type: "Meta", label: "url", id: "url" },
            filterType: { filterValue: "Contains", filterComboBoxValue: "   " },
          },
        ],
      } as SelectedFilterValue;

      const result = getFormattedFilters(survey, selectedFilter, dateRange);

      // A whitespace-only value cannot form a condition; the row drops instead of matching nothing.
      expect(result.reserved?.url).toBeUndefined();
    });

    test("should still handle existing meta filters with array values", () => {
      const selectedFilter: SelectedFilterValue = {
        responseStatuses: [],
        filter: [
          {
            elementType: { type: "Meta", label: "source", id: "source" },
            filterType: { filterValue: "Equals", filterComboBoxValue: ["google"] },
          },
        ],
      } as SelectedFilterValue;

      const result = getFormattedFilters(survey, selectedFilter, dateRange);

      expect(result.reserved?.source).toEqual({ op: "equals", value: "google" });
    });

    test("should handle mixed URL and traditional meta filters", () => {
      const selectedFilter: SelectedFilterValue = {
        responseStatuses: [],
        filter: [
          {
            elementType: { type: "Meta", label: "url", id: "url" },
            filterType: { filterValue: "Contains", filterComboBoxValue: "formbricks.com" },
          },
          {
            elementType: { type: "Meta", label: "source", id: "source" },
            filterType: { filterValue: "Equals", filterComboBoxValue: ["newsletter"] },
          },
        ],
      } as SelectedFilterValue;

      const result = getFormattedFilters(survey, selectedFilter, dateRange);

      expect(result.reserved?.url).toEqual({ op: "contains", value: "formbricks.com" });
      expect(result.reserved?.source).toEqual({ op: "equals", value: "newsletter" });
    });

    test("should filter by quota with screened in status", () => {
      const selectedFilter: SelectedFilterValue = {
        responseStatuses: [],
        filter: [
          {
            elementType: { type: "Quotas", label: "Quota 1", id: "quota1" },
            filterType: { filterValue: undefined, filterComboBoxValue: "Screened in" },
          },
        ],
      } as SelectedFilterValue;

      const result = getFormattedFilters(
        survey,
        selectedFilter,
        {} as Parameters<typeof getFormattedFilters>[2]
      );

      expect(result.quotas?.quota1).toEqual({ op: "screenedIn" });
    });

    test("should filter by quota with screened out status", () => {
      const selectedFilter: SelectedFilterValue = {
        responseStatuses: [],
        filter: [
          {
            elementType: { type: "Quotas", label: "Quota 1", id: "quota1" },
            filterType: { filterValue: undefined, filterComboBoxValue: "Screened out (overquota)" },
          },
        ],
      } as SelectedFilterValue;

      const result = getFormattedFilters(
        survey,
        selectedFilter,
        {} as Parameters<typeof getFormattedFilters>[2]
      );

      expect(result.quotas?.quota1).toEqual({ op: "screenedOut" });
    });

    test("should filter by quota with not in quota status", () => {
      const selectedFilter: SelectedFilterValue = {
        responseStatuses: [],
        filter: [
          {
            elementType: { type: "Quotas", label: "Quota 1", id: "quota1" },
            filterType: { filterValue: undefined, filterComboBoxValue: "Not in quota" },
          },
        ],
      } as SelectedFilterValue;

      const result = getFormattedFilters(
        survey,
        selectedFilter,
        {} as Parameters<typeof getFormattedFilters>[2]
      );

      expect(result.quotas?.quota1).toEqual({ op: "screenedOutNotInQuota" });
    });

    test("should filter by multiple quotas with different statuses", () => {
      const selectedFilter: SelectedFilterValue = {
        responseStatuses: [],
        filter: [
          {
            elementType: { type: "Quotas", label: "Quota 1", id: "quota1" },
            filterType: { filterValue: undefined, filterComboBoxValue: "Screened in" },
          },
          {
            elementType: { type: "Quotas", label: "Quota 2", id: "quota2" },
            filterType: { filterValue: undefined, filterComboBoxValue: "Not in quota" },
          },
        ],
      } as SelectedFilterValue;

      const result = getFormattedFilters(
        survey,
        selectedFilter,
        {} as Parameters<typeof getFormattedFilters>[2]
      );

      expect(result.quotas?.quota1).toEqual({ op: "screenedIn" });
      expect(result.quotas?.quota2).toEqual({ op: "screenedOutNotInQuota" });
    });
  });

  describe("buildDateFieldCondition", () => {
    test("a day-granular value becomes the day-wide window it means", () => {
      const min = "2026-09-01";
      const max = "2026-09-02";

      expect(buildDateFieldCondition("equals", min)).toEqual({ op: "inRange", min, max });
      expect(buildDateFieldCondition("notEquals", min)).toEqual({ op: "notInRange", min, max });
      // "after 1 Sep" starts at the next day, so an instant *on* 1 Sep is not after it (ENG-3232).
      expect(buildDateFieldCondition("greaterThan", min)).toEqual({ op: "greaterEqual", value: max });
      // "before 1 Sep" already ends at the bare date: every stored value on that day sorts at or
      // after it, so this one arm needs no rewriting.
      expect(buildDateFieldCondition("lessThan", min)).toEqual({ op: "lessThan", value: min });
    });

    test("the last representable day is still a day, not a point", () => {
      // `new Date("9999-12-31Z")` plus a day is year 10000, which `toISOString` writes in ISO 8601's
      // expanded form (`+010000-01-01T…`). Sliced to ten characters that is `+010000-01`, and `+`
      // sorts below every digit — so as a `max` it would empty the window. Comparing against the
      // bare date instead is the other failure: `9999-12-31T10:30:00Z` is an instant *on* that day,
      // and it would miss "on" and match "after". The bound only has to sort above every value the
      // day can hold, and it is never a stored value itself, so it does not have to be a real date.
      const lastDay = "9999-12-31";
      const bound = "9999-12-32";
      const instantOnLastDay = "9999-12-31T10:30:00.000Z";

      expect(buildDateFieldCondition("equals", lastDay)).toEqual({
        op: "inRange",
        min: lastDay,
        max: bound,
      });
      expect(buildDateFieldCondition("notEquals", lastDay)).toEqual({
        op: "notInRange",
        min: lastDay,
        max: bound,
      });
      expect(buildDateFieldCondition("greaterThan", lastDay)).toEqual({
        op: "greaterEqual",
        value: bound,
      });
      expect(buildDateFieldCondition("lessThan", lastDay)).toEqual({ op: "lessThan", value: lastDay });

      // The property the bound exists for, read the way Postgres compares the strings.
      expect(instantOnLastDay >= lastDay && instantOnLastDay < bound).toBe(true);
      expect(instantOnLastDay >= bound).toBe(false);
      // And nothing can be stored on the bound itself.
      expect(coerceToEmbeddedDataType(bound, "date")).toBeUndefined();
    });

    test("the window covers both spellings a date field stores", () => {
      const condition = buildDateFieldCondition("equals", "2026-09-01");
      if (condition?.op !== "inRange") throw new Error("expected a range condition");

      // Asserted against the ingest contract rather than hand-written strings: these are the exact
      // values the column holds for `?signup_date=…`, and `>=`/`<` on them is the lexicographic
      // comparison Postgres runs. A datetime upper bound would fail the last row — "2026-09-02"
      // sorts before "2026-09-02T00:00:00.000Z", so a stored date-only 2 Sep would land in 1 Sep's
      // window.
      const stored = (raw: string) => normalizeIngestedValue(raw, "date")?.value as string;
      const isInWindow = (raw: string) => stored(raw) >= condition.min && stored(raw) < condition.max;

      expect(isInWindow("2026-09-01")).toBe(true);
      expect(isInWindow("2026-09-01T10:30:00Z")).toBe(true);
      expect(isInWindow("2026-09-01T00:00:00.000Z")).toBe(true);
      expect(isInWindow("2026-09-01T23:59:59.999Z")).toBe(true);
      expect(isInWindow("2026-08-31T23:59:59Z")).toBe(false);
      expect(isInWindow("2026-09-02")).toBe(false);
    });

    test("the day after rolls over months, years and a leap day", () => {
      const maxOf = (value: string) => {
        const condition = buildDateFieldCondition("equals", value);
        return condition?.op === "inRange" ? condition.max : null;
      };

      expect(maxOf("2026-09-30")).toBe("2026-10-01");
      expect(maxOf("2026-12-31")).toBe("2027-01-01");
      expect(maxOf("2024-02-28")).toBe("2024-02-29");
      expect(maxOf("2024-02-29")).toBe("2024-03-01");
    });

    test("the window is UTC wherever the machine running it happens to be", () => {
      // The boundary is read off the UTC clock on purpose: `new Date(2026, 8, 1)` is midnight
      // wherever the process sits, so in a positive-offset zone it lands on the previous day and
      // the same saved filter would answer differently per viewer.
      //
      // Pinned under a zone that is not UTC, deliberately. Under UTC the local-zone and UTC
      // constructors agree to the day, so the mistake is invisible there — and UTC is what CI runs,
      // which would leave this the one guarantee nothing could catch.
      /* eslint-disable turbo/no-undeclared-env-vars -- the zone under test, not app config */
      const original = process.env.TZ;
      process.env.TZ = "Australia/Sydney";

      try {
        expect(buildDateFieldCondition("equals", "2026-09-01")).toEqual({
          op: "inRange",
          min: "2026-09-01",
          max: "2026-09-02",
        });
      } finally {
        process.env.TZ = original;
      }
      /* eslint-enable turbo/no-undeclared-env-vars */
    });

    test("a full datetime names its own instant, so it is compared as-is", () => {
      const value = "2026-09-01T10:30:00Z";

      expect(buildDateFieldCondition("equals", value)).toEqual({ op: "equals", value });
      expect(buildDateFieldCondition("notEquals", value)).toEqual({ op: "notEquals", value });
      expect(buildDateFieldCondition("lessThan", value)).toEqual({ op: "lessThan", value });
      expect(buildDateFieldCondition("greaterThan", value)).toEqual({ op: "greaterThan", value });
    });

    test("a value outside the stored subset, or an operator the menu never offers, drops the row", () => {
      // Each of these would otherwise cut the ISO ordering at a point no stored value sits on.
      for (const value of [
        "2026-02-30", // a day that does not exist
        "01/09/2026",
        "2026-9-1",
        "2026-09-01T10:30:00+02:00", // an offset datetime: the read seam takes UTC only
        "2026-09-01T10:30:00", // and no zone-less one either
        "tomorrow",
      ]) {
        expect(buildDateFieldCondition("equals", value)).toBeNull();
      }

      expect(buildDateFieldCondition("contains", "2026-09-01")).toBeNull();
    });
  });

  describe("getTodayDate", () => {
    test("should return today's date with time set to end of day", () => {
      const today = new Date();
      const result = getTodayDate();

      expect(result.getFullYear()).toBe(today.getFullYear());
      expect(result.getMonth()).toBe(today.getMonth());
      expect(result.getDate()).toBe(today.getDate());
      expect(result.getHours()).toBe(23);
      expect(result.getMinutes()).toBe(59);
      expect(result.getSeconds()).toBe(59);
      expect(result.getMilliseconds()).toBe(999);
    });
  });
});
