import {
  getSessionBindingService,
  type ConversationRef,
  type SessionBindingBindInput,
  type SessionBindingCapabilities,
  type SessionBindingRecord,
  type SessionBindingUnbindInput,
} from "../infra/outbound/session-binding-service.js";

// Shared binding record helpers used by both configured bindings and
// runtime-created plugin conversation bindings.
export async function createConversationBindingRecord(
  input: SessionBindingBindInput,
): Promise<SessionBindingRecord> {
  return await getSessionBindingService().bind(input);
}

export function getConversationBindingCapabilities(params: {
  channel: string;
  accountId: string;
}): SessionBindingCapabilities {
  return getSessionBindingService().getCapabilities(params);
}

export function listSessionBindingRecords(targetSessionKey: string): SessionBindingRecord[] {
  return getSessionBindingService().listBySession(targetSessionKey);
}

export function resolveConversationBindingRecord(
  conversation: ConversationRef,
): SessionBindingRecord | null {
  return getSessionBindingService().resolveByConversation(conversation);
}

export function touchConversationBindingRecord(bindingId: string, at?: number): void {
  const service = getSessionBindingService();
  if (typeof at === "number") {
    service.touch(bindingId, at);
    return;
  }
  service.touch(bindingId);
}

export async function unbindConversationBindingRecord(
  input: SessionBindingUnbindInput,
): Promise<SessionBindingRecord[]> {
  return await getSessionBindingService().unbind(input);
}
