import { InfrastructureError } from "../../errors";

interface IApiErrorOptions {
  status: number;
  title: string;
  detail: string;
  type?: string;
  instance?: string;
  errors?: any[];
  [key: string]: any; // Para permitir añadir campos extra
}

export class ApiError extends InfrastructureError {
  public status: number;
  public title: string;
  public detail: string;
  public type: string;
  public instance?: string;
  public errors?: any[];
  public timestamp: string;

  constructor(options: IApiErrorOptions) {
    super(options.title);

    // Asegura que la instancia sea reconocida correctamente como ApiError
    Object.setPrototypeOf(this, ApiError.prototype);

    // Campos obligatorios
    this.status = options.status;
    this.title = options.title;
    this.detail = options.detail;
    this.timestamp = new Date().toISOString();

    // Campos opcionales con valores por defecto
    this.type = options.type ?? "about:blank";
    this.instance = options.instance;
    this.errors = options.errors;
  }
}
