export interface AppErrorOptions {
  /** Machine-readable error code (the notification key), sent to the client. */
  code?: string;
  /** Optional structured payload (e.g. field-level validation info). */
  details?: unknown;
  /** The underlying error, kept for logging (never sent to the client). */
  cause?: unknown;
}

/**
 * Base class for all expected (operational) application errors. The centralized
 * error middleware turns an `AppError` into a `{ message, code?, details? }`
 * JSON body with `status` as the HTTP status — the shape the frontend
 * `ApiError` consumes. Unexpected/native errors are treated as 500s.
 */
export class AppError extends Error {
  readonly status: number;
  readonly code?: string;
  readonly details?: unknown;

  constructor(status: number, message: string, options: AppErrorOptions = {}) {
    super(message, options.cause !== undefined ? { cause: options.cause } : {});
    this.name = new.target.name;
    this.status = status;
    this.code = options.code;
    this.details = options.details;
  }
}

export default AppError;
