/**
 * Base Execution Engine utility functions
 * Shared utilities used across all Execution Engine resources
 */

import { zodToJsonSchema } from '@alcyone-labs/zod-to-json-schema'
import type { z } from 'zod'
import type { ContractStructure } from './types'

/**
 * Convert Zod schemas to JSON Schema for serialization
 * Used by Workflow and Agent getStructure() methods
 *
 * @param inputSchema - Zod input schema
 * @param outputSchema - Zod output schema (optional for side-effect resources)
 * @param resourceId - Resource identifier for schema naming
 * @returns JSON Schema representation of the contract
 */
export function convertContractToJson(
  inputSchema: z.ZodSchema,
  outputSchema: z.ZodSchema | undefined,
  resourceId: string
): ContractStructure {
  const options = {
    $refStrategy: 'none' as const,
    errorMessages: true
  }

  return {
    inputSchema: zodToJsonSchema(inputSchema, {
      ...options,
      name: `${resourceId}-input`
    }),
    outputSchema: outputSchema
      ? zodToJsonSchema(outputSchema, {
          ...options,
          name: `${resourceId}-output`
        })
      : undefined
  }
}
