/**
 * Custom error classes for the Effigenix API client
 */

export interface ValidationErrorDetail {
  field: string;
  message: string;
  code?: string;
}

/**
 * Base error for all API errors.
 * Wraps HTTP error responses from the backend.
 */
export class ApiError extends Error {
  readonly status: number;
  readonly code?: string;
  readonly validationErrors?: ValidationErrorDetail[];

  constructor(
    message: string,
    status: number,
    code?: string,
    validationErrors?: ValidationErrorDetail[],
  ) {
    super(message);
    this.name = 'ApiError';
    this.status = status;
    if (code !== undefined) this.code = code;
    if (validationErrors !== undefined) this.validationErrors = validationErrors;
  }
}

/**
 * Thrown when the server returns 401 (Unauthorized) or 403 (Forbidden).
 */
export class AuthenticationError extends ApiError {
  constructor(message: string = 'Authentication required', status: number = 401) {
    super(message, status);
    this.name = 'AuthenticationError';
  }
}

/**
 * Thrown when there is no network connection or the request times out.
 */
export class NetworkError extends Error {
  readonly isTimeout: boolean;

  constructor(message: string = 'Network error', isTimeout: boolean = false) {
    super(message);
    this.name = 'NetworkError';
    this.isTimeout = isTimeout;
  }
}

/**
 * Thrown when the refresh token has expired or is invalid.
 * The user must re-authenticate.
 */
export class RefreshTokenExpiredError extends AuthenticationError {
  constructor() {
    super('Session expired. Please log in again.', 401);
    this.name = 'RefreshTokenExpiredError';
  }
}
