import { CustomError } from "./CustomError";

/**
 * UnauthorizedError is a custom error class that extends CustomError.
 * It represents an error when a user is not authorized to perform an action.
 */
export default class UnauthorizedError extends CustomError {
  // HTTP status code for unauthorized error
  private static readonly _statusCode = 401;
  private readonly _code: number;
  private readonly _logging: boolean;
  private readonly _context: { [key: string]: any };

  /**
   * Constructs a new UnauthorizedError instance.
   * @param params - Optional parameters for the error.
   * @param params.code - Custom error code (default is 401).
   * @param params.message - Custom error message (default is "You are not authorized").
   * @param params.logging - Flag to indicate if the error should be logged (default is false).
   * @param params.context - Additional context for the error.
   */
  constructor(params?: { code?: number; message?: string; logging?: boolean; context?: { [key: string]: any } }) {
    const { code, message, logging } = params || {};

    super(message || "You are not authorized");
    this._code = code || UnauthorizedError._statusCode;
    this._logging = logging || false;
    this._context = params?.context || {};

    Object.setPrototypeOf(this, UnauthorizedError.prototype);
  }

  /**
   * Returns an array of error details.
   * @returns An array containing the error message and context.
   */
  get errors() {
    return [{ message: this.message, context: this._context }];
  }

  /**
   * Returns the HTTP status code for the error.
   * @returns The status code.
   */
  get statusCode() {
    return this._code;
  }

  /**
   * Indicates if the error should be logged.
   * @returns True if the error should be logged, otherwise false.
   */
  get logging() {
    return this._logging;
  }
}
