/**
 * API client types and error classes.
 */

/**
 * Custom error class for API errors.
 *
 * Provides structured error information including HTTP status code,
 * error message, and optional detail from the backend.
 */
export class ApiError extends Error {
  constructor(
    public readonly status: number,
    public readonly statusText: string,
    public readonly detail?: string,
    public readonly code?: string
  ) {
    super(detail || statusText)
    this.name = 'ApiError'
  }

  /**
   * Check if this is an authentication error (401).
   */
  get isUnauthorized(): boolean {
    return this.status === 401
  }

  /**
   * Check if this is a forbidden error (403).
   */
  get isForbidden(): boolean {
    return this.status === 403
  }

  /**
   * Check if this is a not found error (404).
   */
  get isNotFound(): boolean {
    return this.status === 404
  }

  /**
   * Check if this is a validation error (422).
   */
  get isValidationError(): boolean {
    return this.status === 422
  }

  /**
   * Check if this is a server error (5xx).
   */
  get isServerError(): boolean {
    return this.status >= 500
  }
}

/**
 * Options for API requests.
 */
export interface RequestOptions {
  /** Skip automatic auth header injection */
  skipAuth?: boolean
  /** Custom headers to merge with defaults */
  headers?: Record<string, string>
  /** Request body (will be JSON serialized) */
  body?: unknown
  /** Query parameters */
  params?: Record<string, string | number | boolean | undefined>
  /** AbortSignal for request cancellation */
  signal?: AbortSignal
}

/**
 * Standard API response wrapper from backend.
 */
export interface ApiResponse<T> {
  data: T
  message?: string
}

/**
 * Paginated response from backend.
 */
export interface PaginatedResponse<T> {
  items: T[]
  total: number
  page: number
  pageSize: number
  totalPages: number
}

/**
 * Backend error response shape.
 */
export interface ErrorResponse {
  detail?: string
  message?: string
  code?: string
}
