import type {
  ChannelAdapterHealthDto,
  ChannelEgressDto,
  ChannelIngressDto,
} from './channel.dto.js';

export type ChannelDeliveryErrorCode =
  | 'invalid_route'
  | 'destination_unavailable'
  | 'delivery_failed';

/** Terminal adapter delivery failure surfaced to the gateway/caller. */
export class ChannelDeliveryError extends Error {
  readonly name = 'ChannelDeliveryError';

  constructor(
    readonly code: ChannelDeliveryErrorCode,
    message: string,
    readonly retryable = false,
    options?: ErrorOptions,
  ) {
    super(message, options);
  }
}

/** Gateway policy boundary consumed by official channel adapters. */
export interface ChannelIngressPort {
  receive(ingress: ChannelIngressDto): Promise<void>;
}

/** Adapter egress boundary used by the gateway after agent output is ready. */
export interface ChannelEgressPort {
  send(egress: ChannelEgressDto): Promise<void>;
}

/** Shared lifecycle seam implemented by every official channel adapter. */
export interface OfficialChannelAdapter {
  readonly name: string;
  start(): Promise<void>;
  stop(): Promise<void>;
  /** Health is best-effort and never throws for ordinary disconnected state. */
  health(): Promise<ChannelAdapterHealthDto>;
}
