// {{ display_name }} MCP server — generated by mcpify. Do not hand-edit.

import { z } from 'zod';

import type { AuthManager } from '../auth/auth-manager.js';
import type { RequestCredentials } from '../auth/request-credentials.js';
import type { Config } from '../core/config-schema.js';
import { NotFoundError } from '../core/errors.js';
import { getEndpoint, type Store } from '../data/store-repository.js';
import { ApiClient } from '../services/api-client.js';
import { validateInput, validateOutput } from '../validation/validator.js';

export const callInputShape = {
  operationId: z.string().min(1).describe('operationId returned by search'),
  arguments: z.record(z.unknown()).default({}).describe('Operation parameters and/or request body'),
};

/**
 * Validates arguments against the input schema, injects the active auth
 * strategy's credentials, executes the live HTTP request, and validates
 * the response against the output schema before returning it — PRD §1.5's
 * `call` pipeline (architecture.md's 4-step `call` pipeline), protecting
 * the calling agent from upstream API drift.
 */
export async function callOperation(
  store: Store,
  config: Config,
  authManager: AuthManager,
  operationId: string,
  args: Record<string, unknown>,
  requestOverride?: RequestCredentials,
): Promise<unknown> {
  const endpoint = getEndpoint(store, operationId);
  if (!endpoint) {
    throw new NotFoundError(`unknown operationId '${operationId}'`);
  }

  validateInput(operationId, args);

  const client = new ApiClient(config, authManager);
  const response = await client.execute(endpoint, args, requestOverride);

  validateOutput(operationId, response.data);
  return response.data;
}
