import type { ContentfulStatusCode } from 'hono/utils/http-status';

export class CustomError extends Error {
  constructor(
    public statusCode: ContentfulStatusCode,
    message?: string,
    public returnValue?: any,
  ) {
    super(message);
  }
}

export function createError(statusCode: ContentfulStatusCode, message: string, returnValue?: any) {
  return new CustomError(statusCode, message, returnValue);
}

export function BAD_REQUEST(message?: string) {
  return new CustomError(400, message || 'Bad Request');
}

export function UNAUTHORIZED(message?: string) {
  return new CustomError(401, message || 'Unauthorized');
}

export function BAD_CREDENTIALS(message?: string) {
  return new CustomError(401, message || 'Bad Credentials');
}

export function FORBIDDEN(message?: string) {
  return new CustomError(403, message || 'Forbidden');
}

export function NOT_FOUND(message?: string) {
  return new CustomError(404, message || 'Not Found');
}

export function METHOD_NOT_ALLOWED(message?: string) {
  return new CustomError(405, message || 'Method Not Allowed');
}

export function CONFLICT(message?: string) {
  return new CustomError(409, message || 'Conflict');
}

export function GONE(message?: string) {
  return new CustomError(410, message || 'Gone');
}

export function INTERNAL_SERVER_ERROR(message?: string, returnValue?: any) {
  return new CustomError(500, message || 'Internal Server Error', returnValue);
}

export function USER_ALREADY_EXISTS(message?: string) {
  return new CustomError(409, message || 'This email address already exists');
}

export function AUDIENCE_ERROR(message?: string) {
  return new CustomError(400, message || 'Failed to create audience in Google Analytics');
}

export const NO_START_EXPERIMENTS = () =>
  new CustomError(
    405,
    "Starting Experiments is temporarily paused. Sorry for the Inconvenience. We will send you an email as soon as it's up again",
  );

export function TOO_MANY_REQUESTS(message?: string, retryAfter?: number) {
  return new CustomError(429, message || 'Too many requests', { retryAfter });
}
