/**
 * @agentguard/sdk — Typed error classes.
 */

import type { ProblemDetail } from "./types.js";

/**
 * Base error thrown by the AgentGuard SDK.
 */
export class AgentGuardError extends Error {
  constructor(message: string) {
    super(message);
    this.name = "AgentGuardError";
  }
}

/**
 * HTTP error with RFC 7807 ProblemDetail body (when the server returns one).
 */
export class AgentGuardAPIError extends AgentGuardError {
  public readonly status: number;
  public readonly detail: ProblemDetail | null;

  constructor(status: number, message: string, detail: ProblemDetail | null = null) {
    super(message);
    this.name = "AgentGuardAPIError";
    this.status = status;
    this.detail = detail;
  }
}

/**
 * Thrown when the request times out.
 */
export class AgentGuardTimeoutError extends AgentGuardError {
  constructor(timeoutMs: number) {
    super(`Request timed out after ${timeoutMs}ms`);
    this.name = "AgentGuardTimeoutError";
  }
}

/**
 * Thrown when the server is unreachable.
 */
export class AgentGuardConnectionError extends AgentGuardError {
  constructor(url: string, cause?: Error) {
    super(`Failed to connect to ${url}: ${cause?.message ?? "unknown error"}`);
    this.name = "AgentGuardConnectionError";
  }
}
