import { HTTPException } from "hono/http-exception";
import type { ContentfulStatusCode } from "hono/utils/http-status";

export function throwException(error: unknown) {
  if (error instanceof HTTPException) {
    throw new HTTPException(error.status, {
      message: error.message,
    });
  }
  throw new HTTPException(500, { message: "Internal server error" });
}

class BaseError extends HTTPException {
  constructor(message: string, status: ContentfulStatusCode) {
    super(status, { message });
  }
}

export class MemberAccountNotFound extends BaseError {
  constructor() {
    super("Member has no account", 404);
  }
}

export class InvalidCredentials extends BaseError {
  constructor() {
    super("Invalid credentials", 401);
  }
}

export class MemberAlreadyExist extends BaseError {
  constructor() {
    super("Member already exist", 409);
  }
}

export class Unauthorised extends BaseError {
  constructor() {
    super("You are not authorized", 403);
  }
}

export class NotAuthenticated extends BaseError {
  constructor() {
    super("Authentication failed", 401);
  }
}
export class BadRequest extends BaseError {
  constructor() {
    super("Bad request. Invalid request", 400);
  }
}

export class InternalServerError extends BaseError {
  constructor() {
    super("Internal server error", 500);
  }
}

export class NotFoundError extends BaseError {
  constructor() {
    super("Not found", 404);
  }
}

export class ContactLimitError extends BaseError {
  constructor() {
    super("Contact limit exceeded", 400);
  }
}

export class DatabaseError extends Error {
  name: string;

  error: unknown;

  constructor({
    name,
    error,
    message,
  }: {
    message?: string;
    name?: string;
    error: unknown;
  }) {
    super(message || "Failed to execute db ops");
    this.name = name || "DatabaseError";
    this.error = error;
  }
}
