import type { AgentSdkAdapter } from "../../registry/AgentSdkAdapter.ts";
import type { AgentHostAdapter } from "../contracts/AgentHostAdapter.ts";
import type { HostDescriptor } from "../contracts/HostDescriptor.ts";
import type { HostEventEnvelope } from "../contracts/HostEvent.ts";
import type { HostFailure } from "../contracts/HostFailure.ts";
import type { HostRunRef, StartRunInput } from "../contracts/HostRun.ts";
import type {
  HostSessionRef,
  HostSessionState,
  PrepareSessionInput,
  PreparedHostSession,
} from "../contracts/HostSession.ts";
import { normalizeHostCapabilities } from "../contracts/HostCapabilities.ts";
import {
  LegacySdkHostAdapter,
  type LegacySdkHostAdapterOptions,
} from "./LegacySdkHostAdapter.ts";

export const DOUBAO_HOST_CAPABILITIES = normalizeHostCapabilities({
  level: "H2",
  persistentSession: false,
  resumableSession: false,
  structuredEvents: true,
  cancellableRun: true,
  inspectableSession: true,
  workspaceBinding: true,
  concurrentWorkers: true,
  extensions: {
    transport: "volcengine-ark-responses",
    switchboard_mode: "shadow",
    tools: true,
  },
});

export interface DoubaoHostAdapterOptions extends LegacySdkHostAdapterOptions {
  hostId?: string;
}

/** Formal second Host used to verify that Core is not Cursor-bound. */
export class DoubaoHostAdapter implements AgentHostAdapter {
  readonly hostId: string;
  readonly #inner: LegacySdkHostAdapter;
  readonly #displayName: string;
  readonly #enabled: boolean;

  constructor(sdk: AgentSdkAdapter, options: DoubaoHostAdapterOptions = {}) {
    this.hostId = options.hostId?.trim() || "host-doubao-ark";
    this.#displayName = options.displayName?.trim() || "Doubao Ark Host";
    this.#enabled = options.enabled !== false;
    this.#inner = new LegacySdkHostAdapter(sdk, {
      ...options,
      hostId: this.hostId,
      displayName: this.#displayName,
      enabled: this.#enabled,
    });
  }

  get underlyingSdkAdapter(): AgentSdkAdapter { return this.#inner.underlyingSdkAdapter; }

  async describe(): Promise<HostDescriptor> {
    return {
      hostId: this.hostId,
      adapterType: "doubao-ark",
      displayName: this.#displayName,
      enabled: this.#enabled,
      capabilities: { ...DOUBAO_HOST_CAPABILITIES },
      version: "1.3.0",
      metadata: { mode: "shadow", wraps: "DoubaoArkAdapter" },
    };
  }

  prepareSession(input: PrepareSessionInput): Promise<PreparedHostSession> {
    return this.#inner.prepareSession(input);
  }
  inspectSession(reference: HostSessionRef): Promise<HostSessionState> {
    return this.#inner.inspectSession(reference);
  }
  startRun(session: HostSessionRef, input: StartRunInput): Promise<HostRunRef> {
    return this.#inner.startRun(session, input);
  }
  streamEvents(run: HostRunRef): AsyncIterable<HostEventEnvelope> {
    return this.#inner.streamEvents(run);
  }
  cancelRun(run: HostRunRef, reason: string): Promise<void> {
    return this.#inner.cancelRun(run, reason);
  }
  classifyError(error: unknown): HostFailure {
    const message = error instanceof Error ? error.message : String(error);
    if (/401|403|api[_ -]?key|unauthorized|forbidden/i.test(message)) {
      return { kind: "permission", message, retryable: false, raw: error };
    }
    return this.#inner.classifyError(error);
  }
}
