import { z } from "zod";

export const INVALID_PASSWORD_RESET_TOKEN_ERROR_CODE = "ERR_INVALID_PASSWORD_RESET_TOKEN";

/**
 * Stable, locale-independent marker used when an SSO-recovery resend is asked for after the stored
 * intent is gone — expired, or already consumed.
 *
 * It exists because the server cannot tell those apart from a state id someone made up: all three are
 * "recovery-shaped callback URL, nothing in the store". Reporting success for that case told a user
 * whose link had aged out that a mail was on its way when none was sent, and left restarting SSO
 * sign-in — the only way out — undiscoverable. A made-up state getting the same message costs nothing,
 * since whoever supplied it already knows what they sent.
 */
export const SSO_RECOVERY_LINK_EXPIRED_ERROR_CODE = "ERR_SSO_RECOVERY_LINK_EXPIRED";

/**
 * `?error=` code the SSO-recovery link lands on `/auth/login` with when the account's second factor
 * cannot be produced there: a verified email with 2FA, on an instance that has credential sign-in
 * switched off (`EMAIL_AUTH_DISABLED=1`).
 *
 * The recovery link proves mailbox access only and must not stand in for the factor (ENG-7266), so
 * this configuration leaves the login page with nothing that can produce it: no credential form, and
 * the identity that started recovery is unlinked by definition. A provider already linked to the
 * account is the one way through, which the login copy names before pointing at an administrator.
 */
export const SSO_RECOVERY_SECOND_FACTOR_UNAVAILABLE_ERROR_CODE = "sso_recovery_second_factor_unavailable";

/**
 * The login `?error=` code for a recovery completion reached by a browser that never opened the emailed
 * recovery link (ENG-7310). Only the browser that opened the link can finish recovery, so the copy points
 * at the email. It also says to ignore the email when the reader did not start a sign-in, because the
 * same redirect is where an attacker's link lands the account holder.
 */
export const SSO_RECOVERY_LINK_NOT_OPENED_ERROR_CODE = "sso_recovery_link_not_opened";

class ResourceNotFoundError extends Error {
  statusCode = 404;
  resourceId: string | null;
  resourceType: string;

  constructor(resource: string, id: string | null) {
    super(id ? `${resource} with ID ${id} not found` : `${resource} not found`);
    this.name = "ResourceNotFoundError";
    this.resourceType = resource;
    this.resourceId = id;
  }
}

class InvalidInputError extends Error {
  statusCode = 400;
  constructor(message: string) {
    super(message);
    this.name = "InvalidInputError";
  }
}

class ValidationError extends Error {
  statusCode = 400;
  constructor(message: string) {
    super(message);
    this.name = "ValidationError";
  }
}

class QueryExecutionError extends Error {
  statusCode = 500;
  constructor(message: string) {
    super(message);
    this.name = "QueryExecutionError";
  }
}

class UnknownError extends Error {
  statusCode = 500;
  constructor(message: string) {
    super(message);
    this.name = "UnknownError";
  }
}

class DatabaseError extends Error {
  statusCode = 500;
  constructor(message: string) {
    super(message);
    this.name = "DatabaseError";
  }
}

class UniqueConstraintError extends Error {
  statusCode = 409;
  constructor(message: string) {
    super(message);
    this.name = "UniqueConstraintError";
  }
}

class ForeignKeyConstraintError extends Error {
  statusCode = 409;
  constructor(message: string) {
    super(message);
    this.name = "ForeignKeyConstraintError";
  }
}

/**
 * A client update reached a response that is already finished. Raised inside the write transaction, under
 * the row lock, so it also catches a finish that committed after the caller's own pre-check (ENG-6869).
 * Callers answer it with {@link RESPONSE_ALREADY_FINISHED_ERROR_CODE}.
 */
class ResponseAlreadyFinishedError extends Error {
  statusCode = 400;
  constructor(responseId: string) {
    super(`Response with ID ${responseId} is already finished`);
    this.name = "ResponseAlreadyFinishedError";
  }
}

class OperationNotAllowedError extends Error {
  statusCode = 403;
  constructor(message: string) {
    super(message);
    this.name = "OperationNotAllowedError";
  }
}

class AuthenticationError extends Error {
  statusCode = 401;
  constructor(message: string) {
    super(message);
    this.name = "AuthenticationError";
  }
}

class AuthorizationError extends Error {
  statusCode = 403;
  constructor(message: string) {
    super(message);
    this.name = "AuthorizationError";
  }
}

class TooManyRequestsError extends Error {
  statusCode = 429;
  retryAfter?: number;
  constructor(message: string, retryAfter?: number) {
    super(message);
    this.name = "TooManyRequestsError";
    this.retryAfter = retryAfter;
  }
}

class InvalidPasswordResetTokenError extends Error {
  statusCode = 400;
  code: string;
  reason?: string;
  userId?: string;
  constructor(code = INVALID_PASSWORD_RESET_TOKEN_ERROR_CODE, reason?: string, userId?: string) {
    super(code);
    this.name = "InvalidPasswordResetTokenError";
    this.code = code;
    this.reason = reason;
    this.userId = userId;
  }
}

interface NetworkError {
  code: "network_error";
  message: string;
  status: number;
  url: URL;
  responseMessage?: string;
  details?: Record<string, string | string[] | number | number[] | boolean | boolean[]>;
}

interface ForbiddenError {
  code: "forbidden";
  message: string;
  status: number;
  url: URL;
  responseMessage?: string;
  details?: Record<string, string | string[] | number | number[] | boolean | boolean[]>;
}

export const ZErrorHandler = z.function({ input: [z.any()], output: z.void() });

export {
  ResourceNotFoundError,
  InvalidInputError,
  ValidationError,
  QueryExecutionError,
  DatabaseError,
  UniqueConstraintError,
  UnknownError,
  ForeignKeyConstraintError,
  OperationNotAllowedError,
  ResponseAlreadyFinishedError,
  AuthenticationError,
  AuthorizationError,
  TooManyRequestsError,
  InvalidPasswordResetTokenError,
};
export type { NetworkError, ForbiddenError };

export const FILE_UPLOAD_ERROR_NAMES = {
  INVALID_FILE_NAME: "InvalidFileNameError",
  STORAGE_NOT_CONFIGURED: "StorageNotConfiguredError",
  STORAGE_UPLOAD_FAILED: "StorageUploadFailedError",
  FILE_TOO_LARGE: "FileTooLargeError",
} as const;

/**
 * Error names that represent expected business-logic failures.
 * These are handled gracefully in the UI and should NOT be reported to Sentry.
 */
export const EXPECTED_ERROR_NAMES = new Set([
  "ResourceNotFoundError",
  "AuthorizationError",
  "InvalidInputError",
  "ValidationError",
  "QueryExecutionError",
  "AuthenticationError",
  "OperationNotAllowedError",
  "ResponseAlreadyFinishedError",
  "TooManyRequestsError",
  "InvalidPasswordResetTokenError",
  "UniqueConstraintError",
  "RequestBodyTooLargeError",
  // A survey change refused while creatives open the survey (409); defined in apps/web's creatives module.
  "SurveyLinkedCreativesError",
  // A survey save refused because its segment changed since it was read (409); defined in apps/web's survey service.
  "SurveySegmentChangedError",
]);

/**
 * Check whether an error is an expected business-logic failure.
 * Works with both error instances and serialised errors (where only `name` survives).
 */
export const isExpectedError = (error: Error): boolean => EXPECTED_ERROR_NAMES.has(error.name);

/**
 * Stable, locale-independent marker placed in an ApiErrorResponse's `details.code` when a
 * response update is rejected because the response was already finalized. Clients key off this
 * instead of the human-readable (and potentially localized/reworded) error message.
 */
export const RESPONSE_ALREADY_FINISHED_ERROR_CODE = "response_already_finished";

/**
 * Stable, locale-independent marker used when a Formbricks Cloud sign-up is rejected because the
 * email uses a personal/free/disposable domain. Reused as the sign-up action's `serverError`
 * sentinel and as the `?error=` code on the SSO rejection redirect, so the client can map it to a
 * localized message without depending on the (server-only) blocklist utility.
 */
export const SIGNUP_EMAIL_DOMAIN_BLOCKED_ERROR_CODE = "email_domain_not_allowed";

/**
 * Stable, locale-independent marker used when a password is rejected because it appears in the
 * Have-I-Been-Pwned breach corpus (ENG-1587). Set as the `code` on the Better Auth APIError thrown
 * by the breach-check plugin, then re-surfaced as the sign-up / reset action's `serverError`
 * sentinel so the client can map it to a localized message.
 */
export const PASSWORD_COMPROMISED_ERROR_CODE = "password_compromised";

/**
 * Stable, locale-independent marker used when a sign-up presents an invite token that is malformed,
 * expired, or issued to a different email address than the account being created.
 */
export const INVITE_TOKEN_INVALID_ERROR_CODE = "invite_token_invalid";

/**
 * Stable, locale-independent marker used when a sign-up is rejected because the instance has public
 * sign-up closed (`SIGNUP_DISABLED`, or multi-org disabled) and the caller presented neither a valid
 * invite nor a fresh instance to bootstrap.
 */
export const SIGNUP_DISABLED_ERROR_CODE = "signup_disabled";

export interface ApiErrorResponse {
  code:
    | "not_found"
    | "gone"
    | "bad_request"
    | "internal_server_error"
    | "unauthorized"
    | "method_not_allowed"
    | "not_authenticated"
    | "forbidden"
    | "network_error"
    | "too_many_requests";
  message: string;
  status: number;
  url?: URL;
  details?: Record<string, string | string[] | number | number[] | boolean | boolean[]>;
  responseMessage?: string;
}

/**
 * Error types for UI display
 */
export type ClientErrorType = "rate_limit" | "general";

export interface ClientErrorData {
  /** Error type to determine which translations to use */
  type: ClientErrorType;
  /** Whether to show action buttons */
  showButtons?: boolean;
}

/**
 * Helper function to get error data from any error for UI display
 */
export const getClientErrorData = (error: Error): ClientErrorData => {
  // Check by error name as fallback (in case instanceof fails due to module loading issues)
  if (error.name === "TooManyRequestsError") {
    return {
      type: "rate_limit",
      showButtons: false,
    };
  }

  // Default to general error for any other error
  return {
    type: "general",
    showButtons: true,
  };
};
