import { z } from "zod";
import { ZStorageUrl, isSafeLinkUrl } from "../common";
import { ZI18nString } from "../i18n";
import { ZAllowedFileExtension } from "../storage";
import { TSurveyElementTypeEnum } from "./constants";
import { ZConditionGroup } from "./logic";
import { FORBIDDEN_IDS } from "./validation";
import { ZValidationRules } from "./validation-rules";

/**
 * RE-EXPORTING FOR BACKWARDS COMPATIBILITY AND CONVENIENCE
 *
 * TSurveyElementTypeEnum is defined in `constants.ts` to avoid circular dependencies
 * and ensure that the Zod library is not included in bundles that only need the Enum value.
 *
 * However, we re-export it here so that most consumers (who also need the Zod schemas)
 * can import everything from a single file (`elements.ts`).
 */
export { TSurveyElementTypeEnum } from "./constants";

// Element ID validation (same rules as questions - USER EDITABLE)
export const ZSurveyElementId = z.string().superRefine((id, ctx) => {
  if (FORBIDDEN_IDS.includes(id)) {
    ctx.addIssue({
      code: "custom",
      message: `Element id is not allowed`,
    });
  }

  if (id.includes(" ")) {
    ctx.addIssue({
      code: "custom",
      message: "Element id not allowed, avoid using spaces.",
    });
  }

  if (!/^[a-zA-Z0-9_-]+$/.test(id)) {
    ctx.addIssue({
      code: "custom",
      message: "Element id not allowed, use only alphanumeric characters, hyphens, or underscores.",
    });
  }
});

export type TSurveyElementId = z.infer<typeof ZSurveyElementId>;

// Validation logic operator - determines how multiple validation rules are combined
export const ZValidationLogic = z.enum(["and", "or"]);
export type TValidationLogic = z.infer<typeof ZValidationLogic>;

// Combined validation object that includes both rules and logic
// Uses general TValidationRule[] type instead of element-specific narrowed types
export const ZValidation = z.object({
  rules: ZValidationRules,
  logic: ZValidationLogic.prefault("and"),
});

// Base element (like ZSurveyQuestionBase but WITHOUT logic, buttonLabel, backButtonLabel)
// Note: validation is not included in base - each element type will add its own narrowed schema
export const ZSurveyElementBase = z.object({
  id: ZSurveyElementId,
  type: z.enum(TSurveyElementTypeEnum),
  headline: ZI18nString,
  subheader: ZI18nString.optional(),
  imageUrl: ZStorageUrl.optional(),
  // Text alternative for `imageUrl`. Empty or absent renders `alt=""` (decorative); never derived from the
  // file name, which can carry personal data.
  imageAltText: ZI18nString.optional(),
  videoUrl: ZStorageUrl.optional(),
  required: z.boolean(),
  scale: z.enum(["number", "smiley", "star"]).optional(),
  range: z.union([z.literal(5), z.literal(3), z.literal(4), z.literal(7), z.literal(10)]).optional(),
  isDraft: z.boolean().optional(),
  /**
   * Show-if (ENG-6931): the element is shown only when this condition group matches, re-evaluated
   * live as answers above it on the same page change. It may use earlier blocks plus elements above
   * it in the same block. Absent, or an empty group, means always shown.
   */
  showIf: ZConditionGroup.optional(),
});

/**
 * The code an exported file, webhook, prefill and start-at link use instead of the Question ID
 * (ADR 0021). Optional on purpose: surveys stored before codes existed have none and must keep
 * loading. The character rules live in `./codes` and run from `surveyRefinement`, not here, so a
 * stored survey with a bad code is reported rather than made unreadable.
 */
export const ZElementCode = z.string().optional();

// Every element that can be answered. CTA has no answer and therefore no code, so it extends
// `ZSurveyElementBase` directly.
export const ZSurveyQuestionElementBase = ZSurveyElementBase.extend({
  code: ZElementCode,
});

// OpenText Element
export const ZSurveyOpenTextElementInputType = z.enum(["text", "email", "url", "number", "phone"]);
export type TSurveyOpenTextElementInputType = z.infer<typeof ZSurveyOpenTextElementInputType>;

export const ZSurveyOpenTextElement = ZSurveyQuestionElementBase.extend({
  type: z.literal(TSurveyElementTypeEnum.OpenText),
  placeholder: ZI18nString.optional(),
  longAnswer: z.boolean().optional(),
  inputType: ZSurveyOpenTextElementInputType.prefault("text"),
  insightsEnabled: z.boolean().prefault(false).optional(),
  charLimit: z
    .object({
      enabled: z.boolean().prefault(false).optional(),
      min: z.number().optional(),
      max: z.number().optional(),
    })
    .prefault({ enabled: false }),
  validation: ZValidation.optional(),
}).superRefine((data, ctx) => {
  if (data.charLimit.enabled && data.charLimit.min === undefined && data.charLimit.max === undefined) {
    ctx.addIssue({
      code: "custom",
      message: "Enter the values for either minimum or maximum field",
    });
  }

  if (
    (data.charLimit.min !== undefined && data.charLimit.min < 0) ||
    (data.charLimit.max !== undefined && data.charLimit.max < 0)
  ) {
    ctx.addIssue({
      code: "custom",
      message: "The character limit values should be positive",
    });
  }

  if (
    data.charLimit.min !== undefined &&
    data.charLimit.max !== undefined &&
    data.charLimit.min > data.charLimit.max
  ) {
    ctx.addIssue({
      code: "custom",
      message: "Minimum value cannot be greater than the maximum value",
    });
  }
});

export type TSurveyOpenTextElement = z.infer<typeof ZSurveyOpenTextElement>;

// Consent Element
export const ZSurveyConsentElement = ZSurveyQuestionElementBase.extend({
  type: z.literal(TSurveyElementTypeEnum.Consent),
  label: ZI18nString,
  validation: ZValidation.optional(),
});

export type TSurveyConsentElement = z.infer<typeof ZSurveyConsentElement>;

/**
 * Option show-if and Hidden (ENG-6937, json-shapes §2), on every item a respondent picks from: the
 * choices of single select, multi select and ranking, picture choices, and matrix rows and columns.
 *
 * - `showIf`: the item is shown only when this group matches, re-evaluated live. It may use what its
 *   element's show-if may use: earlier blocks plus elements above the element on its block.
 * - `hidden: true` retires the item on a live survey instead of deleting it (P1, P9). It wins over
 *   `showIf`: the item is never shown and is recorded as not shown, but keeps its id, answer code and
 *   earlier answers. Both stay allowed on published surveys (ADR 0020).
 */
export const ZOptionVisibility = {
  showIf: ZConditionGroup.optional(),
  hidden: z.boolean().optional(),
};

// Multiple Choice Elements
export const ZSurveyElementChoice = z.object({
  id: z.string(),
  label: ZI18nString,
  code: ZElementCode,
  ...ZOptionVisibility,
});

export type TSurveyElementChoice = z.infer<typeof ZSurveyElementChoice>;

export const ZShuffleOption = z.enum([
  "none",
  "all",
  "exceptLast",
  "reverseOrderOccasionally",
  "reverseOrderExceptLast",
]);
export type TShuffleOption = z.infer<typeof ZShuffleOption>;

const emptyStringAsUnset = (value: unknown): unknown => (value === "" ? undefined : value);

/**
 * `shuffleOption` as it must be read back from storage.
 *
 * Surveys exist that hold the empty string here — Radix renders `value=""` as an unset dropdown, so
 * the element looks fine in the editor while publishing fails on it. `prefault` heals only
 * `undefined`, so the empty string is normalised away before the enum sees it (ENG-2833). Any other
 * out-of-enum value still fails, and now reports at this field rather than at the whole element.
 *
 * The trailing `.optional()` / `.prefault()` are what keep the key optional on the element: a
 * preprocess step takes `unknown`, so without them Zod would make `shuffleOption` a required key.
 */
export const ZStoredShuffleOption = z.preprocess(emptyStringAsUnset, ZShuffleOption.optional()).optional();
export const ZStoredShuffleOptionWithDefault = z
  .preprocess(emptyStringAsUnset, ZShuffleOption.prefault("none"))
  .prefault("none");

// "row" is a list laid out side by side, for short choices such as Yes / No.
export const ZMultipleChoiceOptionDisplayType = z.enum(["list", "row", "dropdown"]);
export type TMultipleChoiceOptionDisplayType = z.infer<typeof ZMultipleChoiceOptionDisplayType>;

/**
 * Copied options (ENG-6938, json-shapes §3): the id of the earlier question whose options this one
 * copies. Copied options keep the source's option ids and answer codes, and each carries an ordinary
 * show-if "picked in the source". Choices copy from choices (single select, multi select, ranking),
 * pictures from pictures. The validator checks the source exists, comes earlier and is compatible.
 */
export const ZOptionsCopiedFrom = z.string().optional();

// Multiple Choice Single Element
export const ZSurveyMultipleChoiceSingleElement = ZSurveyQuestionElementBase.extend({
  type: z.literal(TSurveyElementTypeEnum.MultipleChoiceSingle),
  optionsCopiedFrom: ZOptionsCopiedFrom,
  choices: z.array(ZSurveyElementChoice).min(2, {
    error: "Multiple Choice Element must have at least two choices",
  }),
  shuffleOption: ZStoredShuffleOption,
  otherOptionPlaceholder: ZI18nString.optional(),
  displayType: ZMultipleChoiceOptionDisplayType.optional(),
});

// Multiple Choice Multi Element
export const ZSurveyMultipleChoiceMultiElement = ZSurveyQuestionElementBase.extend({
  type: z.literal(TSurveyElementTypeEnum.MultipleChoiceMulti),
  optionsCopiedFrom: ZOptionsCopiedFrom,
  choices: z.array(ZSurveyElementChoice).min(2, {
    error: "Multiple Choice Element must have at least two choices",
  }),
  shuffleOption: ZStoredShuffleOption,
  otherOptionPlaceholder: ZI18nString.optional(),
  validation: ZValidation.optional(),
  displayType: ZMultipleChoiceOptionDisplayType.optional(),
});

// Union type for Multiple Choice Elements
export const ZSurveyMultipleChoiceElement = z.union([
  ZSurveyMultipleChoiceSingleElement,
  ZSurveyMultipleChoiceMultiElement,
]);

export type TSurveyMultipleChoiceElement = z.infer<typeof ZSurveyMultipleChoiceElement>;

// NPS Element
export const ZSurveyNPSElement = ZSurveyQuestionElementBase.extend({
  type: z.literal(TSurveyElementTypeEnum.NPS),
  lowerLabel: ZI18nString.optional(),
  upperLabel: ZI18nString.optional(),
  isColorCodingEnabled: z.boolean().prefault(false),
});

export type TSurveyNPSElement = z.infer<typeof ZSurveyNPSElement>;

// CTA Element
export const ZSurveyCTAElement = ZSurveyElementBase.extend({
  type: z.literal(TSurveyElementTypeEnum.CTA),
  buttonExternal: z.boolean().prefault(false),
  buttonUrl: z.string().optional(),
  ctaButtonLabel: ZI18nString.optional(),
}).superRefine((data, ctx) => {
  // When buttonExternal is true, buttonUrl and ctaButtonLabel are required
  if (data.buttonExternal) {
    if (!data.buttonUrl || data.buttonUrl.trim() === "") {
      ctx.addIssue({
        code: "custom",
        message: "Button URL is required when external button is enabled",
        path: ["buttonUrl"],
      });
    } else if (!isSafeLinkUrl(data.buttonUrl)) {
      // Validate URL format only when buttonExternal is true and URL is provided. The scheme has to be
      // constrained, not just parseable: the renderer hands this value to `window.open()`, so accepting
      // any `z.url()`-parseable value (which includes `javascript:`) made an editable survey field
      // stored XSS — on a link survey that executes on the Formbricks origin.
      ctx.addIssue({
        code: "custom",
        message: "Please enter a valid http(s), mailto: or tel: URL",
        path: ["buttonUrl"],
      });
    }

    if (!data.ctaButtonLabel?.default || data.ctaButtonLabel.default.trim() === "") {
      ctx.addIssue({
        code: "custom",
        message: "Button label is required when external button is enabled",
        path: ["ctaButtonLabel"],
      });
    }
  }
});

export type TSurveyCTAElement = z.infer<typeof ZSurveyCTAElement>;

// Rating Element
export const ZSurveyRatingElement = ZSurveyQuestionElementBase.extend({
  type: z.literal(TSurveyElementTypeEnum.Rating),
  scale: z.enum(["number", "smiley", "star"]),
  range: z.union([z.literal(5), z.literal(3), z.literal(4), z.literal(6), z.literal(7), z.literal(10)]),
  lowerLabel: ZI18nString.optional(),
  upperLabel: ZI18nString.optional(),
  isColorCodingEnabled: z.boolean().prefault(false),
});

export type TSurveyRatingElement = z.infer<typeof ZSurveyRatingElement>;

// Picture Selection Element
export const ZSurveyPictureChoice = z.object({
  id: z.string(),
  code: ZElementCode,
  imageUrl: ZStorageUrl,
  imageAltText: ZI18nString.optional(),
  ...ZOptionVisibility,
});

export type TSurveyPictureChoice = z.infer<typeof ZSurveyPictureChoice>;

export const ZSurveyPictureSelectionElement = ZSurveyQuestionElementBase.extend({
  type: z.literal(TSurveyElementTypeEnum.PictureSelection),
  optionsCopiedFrom: ZOptionsCopiedFrom,
  allowMulti: z.boolean().prefault(false),
  choices: z.array(ZSurveyPictureChoice).min(2, {
    error: "Picture Selection element must have a minimum of 2 choices",
  }),
  validation: ZValidation.optional(),
});

export type TSurveyPictureSelectionElement = z.infer<typeof ZSurveyPictureSelectionElement>;

// Date Element
export const ZSurveyDateElement = ZSurveyQuestionElementBase.extend({
  type: z.literal(TSurveyElementTypeEnum.Date),
  html: ZI18nString.optional(),
  format: z.enum(["M-d-y", "d-M-y", "y-M-d"]),
  validation: ZValidation.optional(),
});

export type TSurveyDateElement = z.infer<typeof ZSurveyDateElement>;

// File Upload Element
export const ZSurveyFileUploadElement = ZSurveyQuestionElementBase.extend({
  type: z.literal(TSurveyElementTypeEnum.FileUpload),
  allowMultipleFiles: z.boolean(),
  maxSizeInMB: z.number().optional(),
  allowedFileExtensions: z.array(ZAllowedFileExtension).optional(),
  validation: ZValidation.optional(),
});

export type TSurveyFileUploadElement = z.infer<typeof ZSurveyFileUploadElement>;

// Signature Element
export const ZSurveySignatureElement = ZSurveyQuestionElementBase.extend({
  type: z.literal(TSurveyElementTypeEnum.Signature),
  allowTypedSignature: z.boolean().prefault(true),
});

export type TSurveySignatureElement = z.infer<typeof ZSurveySignatureElement>;

// Cal Element
export const ZSurveyCalElement = ZSurveyQuestionElementBase.extend({
  type: z.literal(TSurveyElementTypeEnum.Cal),
  calUserName: z.string().min(1, {
    error: "Cal user name is required",
  }),
  calHost: z.string().optional(),
});

export type TSurveyCalElement = z.infer<typeof ZSurveyCalElement>;

// Matrix Element
export const ZSurveyMatrixElementChoice = z.object({
  id: z.string(),
  label: ZI18nString,
  code: ZElementCode,
  ...ZOptionVisibility,
});

export type TSurveyMatrixElementChoice = z.infer<typeof ZSurveyMatrixElementChoice>;

export const ZSurveyMatrixElement = ZSurveyQuestionElementBase.extend({
  type: z.literal(TSurveyElementTypeEnum.Matrix),
  rows: z.array(ZSurveyMatrixElementChoice),
  columns: z.array(ZSurveyMatrixElementChoice),
  shuffleOption: ZStoredShuffleOptionWithDefault,
  validation: ZValidation.optional(),
});

export type TSurveyMatrixElement = z.infer<typeof ZSurveyMatrixElement>;

// Address Element
const ZToggleInputConfig = z.object({
  show: z.boolean(),
  required: z.boolean(),
  placeholder: ZI18nString,
});

export type TInputFieldConfig = z.infer<typeof ZToggleInputConfig>;

export const ZSurveyAddressElement = ZSurveyQuestionElementBase.extend({
  type: z.literal(TSurveyElementTypeEnum.Address),
  addressLine1: ZToggleInputConfig,
  addressLine2: ZToggleInputConfig,
  city: ZToggleInputConfig,
  state: ZToggleInputConfig,
  zip: ZToggleInputConfig,
  country: ZToggleInputConfig,
  validation: ZValidation.optional(),
});

export type TSurveyAddressElement = z.infer<typeof ZSurveyAddressElement>;

// Ranking Element
export const ZSurveyRankingElement = ZSurveyQuestionElementBase.extend({
  type: z.literal(TSurveyElementTypeEnum.Ranking),
  optionsCopiedFrom: ZOptionsCopiedFrom,
  choices: z
    .array(ZSurveyElementChoice)
    .min(2, {
      error: "Ranking Element must have at least two options",
    })
    .max(25, {
      error: "Ranking Element can have at most 25 options",
    }),
  otherOptionPlaceholder: ZI18nString.optional(),
  shuffleOption: ZStoredShuffleOption,
  validation: ZValidation.optional(),
});

export type TSurveyRankingElement = z.infer<typeof ZSurveyRankingElement>;

// Contact Info Element
export const ZSurveyContactInfoElement = ZSurveyQuestionElementBase.extend({
  type: z.literal(TSurveyElementTypeEnum.ContactInfo),
  firstName: ZToggleInputConfig,
  lastName: ZToggleInputConfig,
  email: ZToggleInputConfig,
  phone: ZToggleInputConfig,
  company: ZToggleInputConfig,
  validation: ZValidation.optional(),
});

export type TSurveyContactInfoElement = z.infer<typeof ZSurveyContactInfoElement>;

// CSAT Element
export const ZSurveyCsatElement = ZSurveyQuestionElementBase.extend({
  type: z.literal(TSurveyElementTypeEnum.CSAT),
  scale: z.enum(["number", "smiley", "star"]),
  range: z.literal(5),
  lowerLabel: ZI18nString.optional(),
  upperLabel: ZI18nString.optional(),
  isColorCodingEnabled: z.boolean().prefault(false),
});

export type TSurveyCsatElement = z.infer<typeof ZSurveyCsatElement>;

// CES Element
export const ZSurveyCesElement = ZSurveyQuestionElementBase.extend({
  type: z.literal(TSurveyElementTypeEnum.CES),
  scale: z.enum(["number", "smiley", "star"]),
  range: z.union([z.literal(5), z.literal(7)]),
  lowerLabel: ZI18nString.optional(),
  upperLabel: ZI18nString.optional(),
  isColorCodingEnabled: z.boolean().prefault(false),
});

export type TSurveyCesElement = z.infer<typeof ZSurveyCesElement>;

// Union of all element types.
// Discriminated on `type` on purpose: a plain `z.union` reports a single `invalid_union` issue at the
// element's own path with the bare default message "Invalid input", hiding which field actually failed.
// Discriminating picks the one member that matches and surfaces its inner issue path (e.g.
// `blocks.0.elements.0.rows.1.label.de`), which is what the editor needs to name the broken field.
export const ZSurveyElement = z.discriminatedUnion("type", [
  ZSurveyOpenTextElement,
  ZSurveyConsentElement,
  ZSurveyMultipleChoiceSingleElement,
  ZSurveyMultipleChoiceMultiElement,
  ZSurveyNPSElement,
  ZSurveyCTAElement,
  ZSurveyRatingElement,
  ZSurveyPictureSelectionElement,
  ZSurveyDateElement,
  ZSurveyFileUploadElement,
  ZSurveyCalElement,
  ZSurveyMatrixElement,
  ZSurveyAddressElement,
  ZSurveyRankingElement,
  ZSurveyContactInfoElement,
  ZSurveyCsatElement,
  ZSurveyCesElement,
  ZSurveySignatureElement,
]);

export type TSurveyElement = z.infer<typeof ZSurveyElement>;

export const ZSurveyElements = z.array(ZSurveyElement);
export type TSurveyElements = z.infer<typeof ZSurveyElements>;
