// file: libs/sdk/src/transport/mcp-handlers/read-resource-request.handler.ts

import { ReadResourceRequestSchema, type ReadResourceRequest, type ReadResourceResult } from '@frontmcp/protocol';

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

export default function readResourceRequestHandler({
  scope,
}: McpHandlerOptions): McpHandler<ReadResourceRequest, ReadResourceResult> {
  const logger = scope.logger.child('read-resource-request-handler');

  return {
    requestSchema: ReadResourceRequestSchema,
    handler: async (request: ReadResourceRequest, ctx) => {
      const uri = request.params?.uri || 'unknown';
      logger.info(`resources/read: ${uri}`);
      const start = Date.now();
      try {
        const result = await scope.runFlowForOutput('resources:read-resource', { request, ctx });
        logger.verbose('resources/read completed', { uri, durationMs: Date.now() - start });
        return result;
      } catch (e) {
        logger.error('resources/read failed', {
          uri,
          error: e instanceof Error ? { name: e.name, message: e.message, stack: e.stack } : e,
        });
        // Preserve structured JSON-RPC codes (e.g. AuthorityDeniedError -32003,
        // ResourceNotFoundError -32002) instead of letting the generic dispatch
        // flatten them to -32603 — mirrors the skills/load handler.
        throw toSdkMcpError(e);
      }
    },
  };
}
