import { ListPromptsRequestSchema, type ListPromptsRequest, type ListPromptsResult } from '@frontmcp/protocol';

import { type McpHandler, type McpHandlerOptions } from './mcp-handlers.types';

export default function listPromptsRequestHandler({
  scope,
}: McpHandlerOptions): McpHandler<ListPromptsRequest, ListPromptsResult> {
  const logger = scope.logger.child('list-prompts-request-handler');

  return {
    requestSchema: ListPromptsRequestSchema,
    handler: async (request: ListPromptsRequest, ctx) => {
      logger.verbose('prompts/list requested');
      const start = Date.now();
      try {
        const result = await scope.runFlowForOutput('prompts:list-prompts', { request, ctx });
        logger.verbose('prompts/list completed', { durationMs: Date.now() - start });
        return result;
      } catch (e) {
        logger.error('prompts/list failed', {
          error: e instanceof Error ? { name: e.name, message: e.message, stack: e.stack } : e,
        });
        throw e;
      }
    },
  };
}
