import type { FormulaAST } from "./ast";
import type { DateCell, DateKind } from "./date";

export type FormulaResultType =
  | "number"
  | "string"
  | "boolean"
  | "date"
  | "datetime"
  | "null";

export type FormulaVolatility = "now" | "today" | null;

export type FormulaTypeOptions = {
  source: string;
  ast: FormulaAST;
  resultType: FormulaResultType;
  dependencies: string[];
  astVersion: 1;
  volatile?: FormulaVolatility;
  formatOptions?: Record<string, unknown>;
};

/*
 * The runtime value produced by evaluating a node. Strings and numbers are
 * their JS equivalents; dates are DateValue objects; booleans are booleans; missing or
 * filtered-out values are null. Errors are distinguishable from all valid
 * values because they are objects with a `__err` key.
 */
export type DateValue = { kind: DateKind; value: string };

export type Value = number | string | boolean | null | DateValue | ErrorCell;

export type FormulaCell = number | string | boolean | null | DateCell | ErrorCell;

export type ErrorCell = {
  __err: ErrorCode;
  msg: string;
  v: 1;
};

export type ErrorCode =
  | "MISSING_PROP"
  | "TYPE_MISMATCH"
  | "DIV_BY_ZERO"
  | "DATE_INVALID"
  | "SIZE_LIMIT_EXCEEDED"
  | "DEPTH_EXCEEDED"
  | "DEPENDENCY_ERROR";

/*
 * EvalContext carries everything the evaluator needs that isn't in the AST:
 * the function registry (server-only), the property map for resolving `prop`
 * nodes to their formula ASTs when nested, and the current recursion depth.
 */
export type EvalContext = {
  registry: ReadonlyMap<string, import("./functions/registry").FormulaFn>;
  properties: ReadonlyMap<string, PropertyLookup>;
  depth: number;
  maxDepth: number;
  memo: Map<string, Value>; // keyed by propId for the current row-eval
  path: Set<string>;
  nodes: number;
  textChars: number;
  zone: string;
  now: Date;
  today: string | null;
  createdAt: Date | null;
  updatedAt: Date | null;
};

export type PropertyLookup = {
  id: string;
  type: string;
  typeOptions: unknown;
};

export const DEFAULT_MAX_DEPTH = 64;

/*
 * DoS guards. A formula source longer than MAX_FORMULA_SOURCE_LENGTH is
 * rejected before tokenizing (cheap backstop against pathological input like
 * "(".repeat(50000)). MAX_PARSE_DEPTH bounds recursive-descent nesting so a
 * deeply nested source throws a catchable FormulaParseError instead of
 * overflowing the JS stack with a RangeError. MAX_EVAL_DEPTH bounds the
 * tree-walking evaluator so an oversized AST that slipped past the parser
 * degrades to an error cell instead of crashing the recompute worker.
 */
export const MAX_FORMULA_SOURCE_LENGTH = 10_000;
export const MAX_PARSE_DEPTH = 256;
export const MAX_EVAL_DEPTH = 512;
export const MAX_EVAL_NODES = 100_000;
// Long Text cells and materialized formula results share the same limit.
export const MAX_TEXT_CELL_LENGTH = 25_000;
export const MAX_TEXT_LENGTH = 100_000;
export const MAX_EVAL_TEXT_CHARS = 5_000_000;
