import { z } from "zod";
import type { TElementOptionsNotShown, TOptionListKey } from "./show-if";

/**
 * The not-shown record (ENG-6936, ADR 0029): what a respondent was **not shown**, decided by the
 * survey while they answer and stored on the response as it was at answer time. Results read it
 * instead of re-running today's rules, so editing a show-if mid-field never changes past results.
 *
 * It lists only what was kept from the respondent, by stable ids (locked once published, ADR 0020).
 * "Not listed" means shown or never reached. `null` on a stored response means unknown (a response
 * from before 6.2 or from an older cached bundle), and results read those as before.
 *
 * `options`, `rows` and `columns` are part of the approved shape (json-shapes §8) and filled by
 * option, row and column show-if and Hidden (ENG-6937); the record accepts and stores them now.
 */

/** Ids a list may carry. The same ceiling as a survey's block order: no real survey lists more. */
export const MAX_NOT_SHOWN_IDS = 1000;
/** Elements a per-element map (`options`, `rows`, `columns`) may key. */
export const MAX_NOT_SHOWN_MAP_KEYS = 1000;
/** Characters an id may have. Element, block and option ids are far shorter. */
export const MAX_NOT_SHOWN_ID_LENGTH = 200;

// The record is stored as jsonb, which cannot hold U+0000: refusing it here answers 400, not 500.
const ZNotShownId = z
  .string()
  .min(1)
  .max(MAX_NOT_SHOWN_ID_LENGTH)
  .refine((id) => !id.includes("\u0000"), "must not contain NULL bytes");
const ZNotShownIds = z.array(ZNotShownId).max(MAX_NOT_SHOWN_IDS);
const ZNotShownMap = z
  .record(ZNotShownId, ZNotShownIds)
  .refine((map) => Object.keys(map).length <= MAX_NOT_SHOWN_MAP_KEYS, {
    error: `A not-shown map may key at most ${MAX_NOT_SHOWN_MAP_KEYS} elements`,
  });

/**
 * What the survey sends with every create and update. Closed at every level, and capped, because the
 * client routes are public.
 */
export const ZResponseNotShownInput = z.strictObject({
  /** Block ids skipped by show-if, or because every element on them was hidden. */
  blocks: ZNotShownIds.optional(),
  /** Element ids hidden by show-if, or skipped with no options left. */
  elements: ZNotShownIds.optional(),
  /** Element id → option ids not shown. */
  options: ZNotShownMap.optional(),
  /** Matrix element id → row ids not shown. */
  rows: ZNotShownMap.optional(),
  /** Matrix element id → column ids not shown. */
  columns: ZNotShownMap.optional(),
});

/**
 * A stored record, as read. The same keys without the input caps: a cap applies to what a caller
 * sends and never makes a stored response unreadable.
 */
export const ZResponseNotShown = z.object({
  blocks: z.array(z.string()).optional(),
  elements: z.array(z.string()).optional(),
  options: z.record(z.string(), z.array(z.string())).optional(),
  rows: z.record(z.string(), z.array(z.string())).optional(),
  columns: z.record(z.string(), z.array(z.string())).optional(),
});

export type TResponseNotShown = z.infer<typeof ZResponseNotShown>;

/**
 * How each option list of an element is named outside the element: `recordKey`, its map in the
 * not-shown record (json-shapes §8), and `noun`, one item of it, as a shown / not shown filter targets
 * it (ENG-6951) and as an error message names it. The one table every module reads these from.
 */
export const OPTION_LIST_NAMES = {
  choices: { recordKey: "options", noun: "option" },
  rows: { recordKey: "rows", noun: "row" },
  columns: { recordKey: "columns", noun: "column" },
} as const satisfies Record<TOptionListKey, { recordKey: keyof TResponseNotShown; noun: string }>;

/** One item of an option list: an option, a matrix row or a matrix column. */
export type TOptionItemNoun = (typeof OPTION_LIST_NAMES)[TOptionListKey]["noun"];

const OPTION_LIST_KEYS = ["choices", "rows", "columns"] as const satisfies readonly TOptionListKey[];

/** The option list whose items `noun` names: `choices` for an option, `rows` / `columns` for a matrix. */
export const getOptionListKeyOfNoun = (noun: TOptionItemNoun): TOptionListKey | undefined =>
  OPTION_LIST_KEYS.find((listKey) => OPTION_LIST_NAMES[listKey].noun === noun);

/** `elements` is optional: a stored block can lack it (an unvalidated draft or migrated block, ENG-2497). */
type TBlockLike = { id: string; elements?: readonly { id: string }[] };

/**
 * The elements a record says the respondent was not shown: those it lists, plus every element of the
 * blocks it lists. Only ids of `blocks`' elements come back, so a record naming anything else (a
 * hidden field, the verified email, a deleted element) never reaches the answers through it.
 *
 * `null` or `undefined` (unknown) returns an empty set, which reads as today.
 */
export const getNotShownElementIds = (
  record: TResponseNotShown | null | undefined,
  blocks: readonly TBlockLike[]
): Set<string> => {
  const notShown = new Set<string>();
  if (!record) return notShown;

  const listedBlocks = new Set(record.blocks ?? []);
  const listedElements = new Set(record.elements ?? []);
  if (listedBlocks.size === 0 && listedElements.size === 0) return notShown;

  for (const block of blocks) {
    const wholeBlock = listedBlocks.has(block.id);
    for (const element of block.elements ?? []) {
      if (wholeBlock || listedElements.has(element.id)) notShown.add(element.id);
    }
  }
  return notShown;
};

/**
 * The options, rows and columns the record says one element did not offer, keyed by option list the
 * way show-if keys them (ENG-7332). `null` or `undefined` (unknown) returns `{}`, which reads as today.
 */
export const getElementOptionsNotShownFromRecord = (
  record: TResponseNotShown | null | undefined,
  elementId: string
): TElementOptionsNotShown => {
  const notShown: TElementOptionsNotShown = {};
  if (!record) return notShown;
  for (const listKey of OPTION_LIST_KEYS) {
    const ids = record[OPTION_LIST_NAMES[listKey].recordKey]?.[elementId];
    if (ids && ids.length > 0) notShown[listKey] = ids;
  }
  return notShown;
};
