import type { z } from "zod";
import type { TLinkedEmbeddedField } from "../embedded-data-resolver";
import { RESERVED_FIELD_NAMES } from "../reserved-field-names";
import { type TSurveyHiddenFields, ZSurveyVariables } from "./types";
import {
  LINK_SURVEY_SYSTEM_PARAM_KEYS,
  type TValidateIdError,
  TValidateIdErrorCode,
  validateId,
} from "./validation";

/**
 * The declared-field carriers of a survey write payload, as every write seam spells them: one
 * object literal with every key present, each either populated or `undefined`.
 */
export interface TDeclaredFieldSource {
  /**
   * The V2 carrier (ENG-3228). When a payload declares its fields as rows it declares **all** of
   * them, both sources, so this takes precedence over the two legacy keys below and they are not
   * read at all — the same precedence `resolveDesiredEmbeddedFields` applies in the reconcile. A
   * survey loaded through a select that carries the join always has it, which is what makes the
   * grandfathering set the rows rather than the legacy keys.
   */
  embeddedFields?: TLinkedEmbeddedField[] | null;
  hiddenFields?: Pick<TSurveyHiddenFields, "fieldIds"> | null;
  /**
   * Only `name` is required, deliberately: this reads nothing else, and the write seams hand it both
   * sides of the survey schema. `ZSurveyVariable` gives `value` a `prefault`, so a *create* payload
   * (`ZSurveyCreateInput`) types `value` as optional while a parsed survey types it as required -
   * demanding the *parsed* `TSurveyVariables` here made `createSurvey` fail to type check against
   * its own input, so this is the schema's input side: `value` optional, which a parsed survey
   * (value required) also satisfies.
   */
  variables?: z.input<typeof ZSurveyVariables> | null;
  /**
   * Not a declared-field carrier, but its element ids share `response.data` with the hidden fields
   * (ENG-3142). `unknown` on purpose: the draft save hands the guard blocks no schema has checked.
   */
  blocks?: readonly unknown[] | null;
}

const isDeclared = <T>(carrier: T | null | undefined): carrier is T =>
  carrier !== undefined && carrier !== null;

/**
 * The name one Embedded Data entry occupies in the survey's recall and logic namespace.
 *
 * Not the same attribute for both sources, because the legacy shape is not: a computed field is
 * addressed by its variable *name*, an ingested one by the *storage key* its value arrives under.
 * A shared computed field answers to its library key rather than its display label, for the reason
 * `toLegacyEmbeddedFields` (embedded-data-mapping.ts) puts the key into the derived projection — a
 * label like `Plan tier` is not a legal variable name, and checking it here would refuse every
 * link to a library field that has one.
 */
const declaredEntryName = ({ field, link }: TLinkedEmbeddedField): string =>
  field.source === "computed" ? (field.key ?? field.name) : link.storageKey;

/**
 * The declared field names a payload actually *declares*.
 *
 * Presence is `!== undefined`, deliberately **not** the `in` operator — the same test
 * `resolveDesiredEmbeddedFields` (apps/web/lib/embedded-data/reconcile.ts) uses, and for the same
 * reason: every write seam builds one object literal with both keys spelled out and lets Prisma (or
 * the reconcile) ignore the undefined ones. Under `in`, a patch that never mentioned `hiddenFields`
 * would read as declaring the empty set, and the two modules would disagree about what a payload
 * said — the reconcile carrying the current rows over while the guard treated them as absent.
 *
 * A `null` carrier is treated as absent for the same reason: it declares nothing to validate.
 */
export const collectDeclaredFieldNames = (source: TDeclaredFieldSource): string[] =>
  isDeclared(source.embeddedFields)
    ? source.embeddedFields.map(declaredEntryName)
    : [
        ...(isDeclared(source.variables) ? source.variables.map((variable) => variable.name) : []),
        ...(isDeclared(source.hiddenFields) ? (source.hiddenFields.fieldIds ?? []) : []),
      ];

/**
 * Refuses reserved names for **newly declared** field names, and only for those.
 *
 * This is the create-time layer the lenient load-time schemas cannot be: `ZSurveyHiddenFields` and
 * `ZSurveyVariables` also parse surveys read back out of the database, so they must keep accepting
 * whatever is already stored. `validateId`'s strict mode is the same rule, but until now it was
 * passed from exactly one place (the hidden-fields editor card) — so
 * `PUT /api/v1/management/surveys/<id>` could still create a hidden field named `lang` or `country`
 * that `getHiddenFieldsFromSearchParams` then refuses to fill, leaving it silently empty forever.
 *
 * ## Grandfathering is the whole point
 *
 * Surveys in production already declare `country`, `url`, `source`, `browser`. Their values live at
 * `response.data["country"]`, `#recall:country#` resolves from there, and nothing may be renamed. So
 * a name already in `existing` returns **no error**, whatever it is — the blocklist applies to names
 * this write is *authoring*. Matching is case-insensitive in both directions, because a new `Lang`
 * would collide with the `?lang=` param the link survey reads for itself (only a grandfathered case
 * variant keeps being filled by its exact spelling, see `getHiddenFieldsFromSearchParams`), and
 * because `Country` and `country` would collide in the recall namespace all the same.
 *
 * Duplicate incoming names yield one error each at most — the caller sees one error per bad name.
 *
 * ## Grandfathering is per-save, not permanent — and that is deliberate
 *
 * `existing` is the survey's *current* names, so the reprieve lasts exactly as long as the field does.
 * Delete a grandfathered `country` field, save, and it can no longer be added back: the next write
 * sees an `existing` set without it and refuses the name like any other new declaration. Editing the
 * field, renaming other fields around it, or saving the survey untouched all keep it — only removing
 * it spends the reprieve.
 *
 * That is the intended reading of "already declared". The alternative — remembering every name a
 * survey ever had — would need storage this layer does not have, and would keep a name reserved for a
 * survey that no longer uses it. A survey that gives up its declared `country` gains the auto-captured
 * one in exchange, which is the field the name is supposed to mean from here on.
 *
 * The client-facing message says "fields a survey already has keep working", which is true of the
 * survey as it stands and is what an integrator hitting this on a *different* survey needs to hear.
 * Someone who deleted the field yesterday and is re-adding it today is the one case where that
 * sentence reads as contradicting them; pinned by a test so the behaviour is a decision rather than an
 * accident.
 *
 * @param existing - Every declared field name the survey already has (variables + hidden fields).
 *   Pass `[]` on a create, which authors every name fresh.
 * @param incoming - The declared field names the payload carries, from
 *   {@link collectDeclaredFieldNames}.
 * @returns One error per refused new name, or `[]` when everything is allowed.
 */
export const validateNewDeclaredFieldNames = ({
  existing,
  incoming,
}: {
  existing: string[];
  incoming: string[];
}): TValidateIdError[] => {
  const grandfathered = new Set(existing.map((name) => name.toLowerCase()));
  const seen = new Set<string>();
  const errors: TValidateIdError[] = [];

  for (const name of incoming) {
    const lowered = name.toLowerCase();
    if (grandfathered.has(lowered) || seen.has(lowered)) continue;
    seen.add(lowered);

    // Delegated rather than reimplemented so this guard and the editor card can never drift apart:
    // `validateId`'s strict branch is the single definition of what a new declared name may be. The
    // id lists are empty on purpose — passing the survey's names would report every grandfathered
    // name as a duplicate. A name shared across the two namespaces is a different check, with its
    // own grandfather rule: `validateNewDeclaredFieldClashes`.
    const error = validateId(name, [], [], [], [], { requireSafeIdentifier: true });
    if (error) errors.push(error);
  }

  return errors;
};

/** Lower-cased name → the spelling the payload used. */
type TNamespaceNames = Map<string, string>;

const toNamespace = (names: string[]): TNamespaceNames =>
  new Map(names.map((name) => [name.toLowerCase(), name]));

const entryNames = (embeddedFields: TLinkedEmbeddedField[], source: "computed" | "ingested"): string[] =>
  embeddedFields.filter((entry) => entry.field.source === source).map(declaredEntryName);

const isRecord = (value: unknown): value is Record<string, unknown> =>
  typeof value === "object" && value !== null && !Array.isArray(value);

/** Every element id in `blocks`, skipping whatever is not shaped like one. */
const elementIdsOf = (blocks: readonly unknown[]): string[] =>
  blocks.flatMap((block) => {
    const elements = isRecord(block) ? block.elements : undefined;
    if (!Array.isArray(elements)) return [];

    return elements.flatMap((element: unknown) =>
      isRecord(element) && typeof element.id === "string" ? [element.id] : []
    );
  });

/** Every question code in `blocks`, skipping whatever is not shaped like a coded element. */
const elementCodesOf = (blocks: readonly unknown[]): string[] =>
  blocks.flatMap((block) => {
    const elements = isRecord(block) ? block.elements : undefined;
    if (!Array.isArray(elements)) return [];

    return elements.flatMap((element: unknown) =>
      isRecord(element) && typeof element.code === "string" ? [element.code] : []
    );
  });

type TDeclaredNamespaces = { variables: TNamespaceNames; hiddenFields: TNamespaceNames };

/** What a source declares, split into the two namespaces recall and logic address fields through. */
const namesByNamespace = (source: TDeclaredFieldSource): TDeclaredNamespaces =>
  isDeclared(source.embeddedFields)
    ? {
        variables: toNamespace(entryNames(source.embeddedFields, "computed")),
        hiddenFields: toNamespace(entryNames(source.embeddedFields, "ingested")),
      }
    : {
        variables: toNamespace(
          isDeclared(source.variables) ? source.variables.map((variable) => variable.name) : []
        ),
        hiddenFields: toNamespace(
          isDeclared(source.hiddenFields) ? (source.hiddenFields.fieldIds ?? []) : []
        ),
      };

/**
 * What the survey declares once the write lands. An `embeddedFields` payload is the complete set for
 * both namespaces, so there is nothing to carry over; the per-carrier merge is for the legacy keys,
 * which arrive independently.
 */
const namesAfterWrite = (
  current: TDeclaredNamespaces,
  incoming: TDeclaredFieldSource
): TDeclaredNamespaces =>
  isDeclared(incoming.embeddedFields)
    ? namesByNamespace(incoming)
    : {
        variables: isDeclared(incoming.variables)
          ? namesByNamespace({ variables: incoming.variables }).variables
          : current.variables,
        hiddenFields: isDeclared(incoming.hiddenFields)
          ? namesByNamespace({ hiddenFields: incoming.hiddenFields }).hiddenFields
          : current.hiddenFields,
      };

/**
 * Refuses a name that a variable and a hidden field would share after the write — unless the survey
 * already holds that exact clash.
 *
 * Recall and logic address variables and hidden fields *by name*, so two fields under one name are
 * ambiguous by construction. The editor refuses this in both directions and the v3 reference
 * validation refuses it as `duplicate_identifier`; the v1 management API had no check at all (ENG-2933).
 * Nothing further down catches it either: the reconcile's `assertNoDuplicateStorageKeys` compares
 * storage keys, and a variable is stored under its `id` while a hidden field is stored under its
 * name, so the two never collide there.
 *
 * Grandfathered like reserved names, and per-save for the same reasons ({@link validateNewDeclaredFieldNames}):
 * surveys in production already hold `first_name` as both, and their read-modify-write PUT must keep
 * working. A clash is refused only when the survey did not already hold it — so adding a variable
 * `plan` beside an existing hidden field `plan` is refused, resending an existing pair is not, and
 * dropping one side spends the reprieve. Case-insensitive, matching the editor and v3.
 *
 * A carrier the payload never mentions (`undefined`/`null`) is the survey's current one, exactly as
 * the reconcile carries those rows over — so a payload that sends only `variables` is still checked
 * against the hidden fields it leaves in place. An `embeddedFields` payload mentions both, so there
 * is nothing to carry over and a newly linked shared field is checked against every local name the
 * survey keeps.
 *
 * The error names the side the write introduces (the hidden field, when the variable already
 * existed; the variable otherwise), with the code the editor's Embedded Data card reports for the
 * same clash.
 */
export const validateNewDeclaredFieldClashes = ({
  existing,
  incoming,
}: {
  existing: TDeclaredFieldSource;
  incoming: TDeclaredFieldSource;
}): TValidateIdError[] => {
  const current = namesByNamespace(existing);
  const next = namesAfterWrite(current, incoming);

  const errors: TValidateIdError[] = [];
  for (const [lowered, variableName] of next.variables) {
    const hiddenFieldId = next.hiddenFields.get(lowered);
    if (hiddenFieldId === undefined) continue;
    if (current.variables.has(lowered) && current.hiddenFields.has(lowered)) continue;

    errors.push({
      code: TValidateIdErrorCode.Duplicate,
      field: current.variables.has(lowered) ? hiddenFieldId : variableName,
    });
  }

  return errors;
};

/**
 * Refuses a name that a hidden field and an element (a question) would share after the write —
 * unless the survey already holds that exact clash (ENG-3142).
 *
 * Element ids and hidden field names key the same `response.data`, and the answer always owns the
 * key: the ingest contract drops the hidden field as `element_id_collision`. So the field shows in
 * the editor and the response table, is empty on every response, and nothing tells the author why.
 * The editor and v3 refuse it; the v1 management API did not.
 *
 * Case-insensitive, like the editor, v3 and the variable clash. Only an exact match is dead at
 * storage (`Plan` and `plan` are different keys), but recall and logic address both by name, so a
 * case variant is still ambiguous. Grandfathered and per-save exactly like the variable clash, and
 * blocks the payload does not carry are the survey's current ones.
 *
 * The error names the side the write introduces: the element when the hidden field already existed,
 * the hidden field otherwise.
 */
export const validateNewElementIdClashes = ({
  existing,
  incoming,
}: {
  existing: TDeclaredFieldSource;
  incoming: TDeclaredFieldSource;
}): TValidateIdError[] => {
  const current = namesByNamespace(existing);
  const currentElementIds = toNamespace(elementIdsOf(existing.blocks ?? []));
  const nextHiddenFields = namesAfterWrite(current, incoming).hiddenFields;
  // An empty list counts as not carried: `updateSurveyInternal` writes `blocks` only when the list is
  // non-empty, so `blocks: []` leaves the stored elements in place.
  const nextElementIds =
    isDeclared(incoming.blocks) && incoming.blocks.length > 0
      ? toNamespace(elementIdsOf(incoming.blocks))
      : currentElementIds;

  const errors: TValidateIdError[] = [];
  for (const [lowered, hiddenFieldId] of nextHiddenFields) {
    const elementId = nextElementIds.get(lowered);
    if (elementId === undefined) continue;
    if (currentElementIds.has(lowered) && current.hiddenFields.has(lowered)) continue;

    errors.push({
      code: TValidateIdErrorCode.Duplicate,
      field: current.hiddenFields.has(lowered) ? elementId : hiddenFieldId,
    });
  }

  return errors;
};

/**
 * Refuses a name that an Embedded Data field (hidden field or variable) and a question code would
 * share after the write — unless the survey already holds that exact clash (ENG-6778, ADR 0021).
 *
 * Question codes are the public name of a question in exports, webhooks and prefill links, next to
 * the Embedded Data names, so one name for both would be ambiguous. Grandfathered and per-save like
 * {@link validateNewElementIdClashes}; blocks the payload does not carry (or an empty list) are the
 * survey's current ones. Case-insensitive. The error names the Embedded Data field, so the editor can
 * point at the field being added; `validateSurveyCodes` reports the same clash against the question
 * when it is the code that is new.
 */
export const validateNewQuestionCodeClashes = ({
  existing,
  incoming,
}: {
  existing: TDeclaredFieldSource;
  incoming: TDeclaredFieldSource;
}): TValidateIdError[] => {
  const current = namesByNamespace(existing);
  const currentNames = new Set([...current.variables.keys(), ...current.hiddenFields.keys()]);
  const currentCodes = new Set(elementCodesOf(existing.blocks ?? []).map((code) => code.toLowerCase()));
  const nextCodes =
    isDeclared(incoming.blocks) && incoming.blocks.length > 0
      ? new Set(elementCodesOf(incoming.blocks).map((code) => code.toLowerCase()))
      : currentCodes;
  const next = namesAfterWrite(current, incoming);

  const errors: TValidateIdError[] = [];
  const seen = new Set<string>();
  for (const [lowered, name] of [...next.variables, ...next.hiddenFields]) {
    if (!nextCodes.has(lowered) || seen.has(lowered)) continue;
    seen.add(lowered);
    if (currentNames.has(lowered) && currentCodes.has(lowered)) continue;

    errors.push({ code: TValidateIdErrorCode.Duplicate, field: name });
  }

  return errors;
};

/**
 * Everything a write's declared field names must satisfy, for the write seams that hold a whole
 * survey on both sides (`updateSurvey`, `createSurvey`): no new reserved name, and no new clash
 * between a variable and a hidden field, or a hidden field and an element. One error per bad name —
 * a name refused as reserved is not reported a second time as a clash, and a name in both clashes
 * is reported once.
 *
 * The v3 patch route calls {@link validateNewDeclaredFieldNames} on its own: its reference validation
 * already refuses both clashes as `duplicate_identifier`, so running this there would report them
 * twice.
 */
export const validateNewDeclaredFields = ({
  existing,
  incoming,
}: {
  existing: TDeclaredFieldSource;
  incoming: TDeclaredFieldSource;
}): TValidateIdError[] => {
  const reserved = validateNewDeclaredFieldNames({
    existing: collectDeclaredFieldNames(existing),
    incoming: collectDeclaredFieldNames(incoming),
  });
  const reported = new Set(reserved.map((error) => error.field.toLowerCase()));
  const clashes = [
    ...validateNewDeclaredFieldClashes({ existing, incoming }),
    ...validateNewElementIdClashes({ existing, incoming }),
    ...validateNewQuestionCodeClashes({ existing, incoming }),
  ].filter((error) => {
    const lowered = error.field.toLowerCase();
    if (reported.has(lowered)) return false;
    reported.add(lowered);
    return true;
  });

  return [...reserved, ...clashes];
};

/**
 * One human-readable sentence for a set of refusals, for the error a write path returns to its
 * client. Server-side and non-localized, matching `APP_SURVEY_TRIGGER_REQUIRED_MESSAGE` and the rest
 * of this layer; the editor refuses these names client-side with a translated toast long before a
 * request is made.
 */
const describeReservedReason = (field: string): string => {
  // Order matters: a name in BOTH lists (`source` is the one Tier-1 field that is also a link-survey
  // system param) gets the capture-refusal reason, which is the stronger and still-true statement.
  //
  // The two halves fail for genuinely different reasons, and saying "could never receive a value" for
  // the catalog half would be actively misleading: `RESERVED_FIELD_NAMES` is deliberately kept OUT of
  // the capture-refusal list read by `getHiddenFieldsFromSearchParams`, precisely so `?country=DE`
  // keeps filling the field of a survey that already declares `country`. An integrator told the wrong
  // reason here could go and remove URL params that work.
  //
  // Named after the reserved spelling rather than the incoming name: a refused `userid` collides with
  // `userId`, and the true statement is about that name — a hidden field called `userId` is never
  // filled from the URL, whatever casing the param arrives in. (A grandfathered `UserId` IS filled by
  // `?UserId=`, so the same sentence about the incoming case variant would be false.) "URL contract"
  // rather than "URL parameter" because `FORBIDDEN_IDS` also holds internal ids such as `end` and
  // `welcomeCard`, which no URL carries.
  const systemParamKey = [...LINK_SURVEY_SYSTEM_PARAM_KEYS].find(
    (key) => key.toLowerCase() === field.toLowerCase()
  );
  if (systemParamKey !== undefined) {
    return `it collides with "${systemParamKey}", a name the link-survey URL contract reserves for itself, so a hidden field under that name is never filled from the URL`;
  }

  if (RESERVED_FIELD_NAMES.has(field.toLowerCase())) {
    return "it names an auto-captured system field that every survey can already read by name, so a second field under that name would be ambiguous in recall and logic";
  }

  // `validateId` classified this Reserved, so one of the two sets matched at the time. Reaching here
  // means the sets and this description have drifted; say something true rather than guess which.
  return "it is a reserved name";
};

/**
 * Why a name refused for a reason other than {@link TValidateIdErrorCode.Reserved} was refused, one
 * clause per code.
 *
 * Per code rather than one sentence for "not reserved": the old fallback returned the
 * `NotSafeIdentifier` sentence for every non-reserved code, so a caller sending `Team Size` was told
 * about lowercase letters when the space was the whole problem, and `""` produced the same sentence
 * as a charset violation. Each code now states the check that actually fired — so following the
 * message always moves the caller forward one check instead of around in a circle.
 */
const DECLARED_FIELD_NAME_REASONS: Record<Exclude<TValidateIdErrorCode, "reserved">, string> = {
  [TValidateIdErrorCode.Empty]: "it must not be empty",
  [TValidateIdErrorCode.HasSpaces]: "it must not contain spaces",
  [TValidateIdErrorCode.InvalidChars]: "it may contain only letters, numbers, underscores and hyphens",
  [TValidateIdErrorCode.NotSafeIdentifier]:
    "it must start with a lowercase letter and contain only lowercase letters, numbers and underscores",
  // Reached from the two clash checks (a hidden field sharing a name with a variable or an element),
  // never from `validateNewDeclaredFieldNames`, which passes empty id lists. Worded for that case
  // without assuming it — on a create both sides are new, so "already uses" would be false there.
  [TValidateIdErrorCode.Duplicate]:
    "a second field in this survey would carry that name, and recall and logic address fields by name",
};

/** The client-facing sentence for one refused name: `Field name "x" cannot be used: <reason>.` */
export const describeDeclaredFieldNameError = (error: TValidateIdError): string => {
  const reason =
    error.code === TValidateIdErrorCode.Reserved
      ? describeReservedReason(error.field)
      : DECLARED_FIELD_NAME_REASONS[error.code];

  return `Field name "${error.field}" cannot be used: ${reason}.`;
};

/**
 * The full client-facing message for a set of refusals. Names a survey already declares are never in
 * here — they are grandfathered — so the trailing sentence is what tells an integrator why their
 * *other* survey with the same field name keeps working.
 */
export const describeDeclaredFieldNameErrors = (errors: TValidateIdError[]): string =>
  `${errors.map(describeDeclaredFieldNameError).join(" ")} Fields a survey already has keep working; this applies to newly added names only.`;
