import type { GatewayRequest, OutboundMessage } from '../protocol.js';
import { makeError, makeResponse, ErrorCode } from '../protocol.js';
import type { ComponentRegistry } from '../../intents/index.js';

export interface IntentHandlerDeps {
  intentRegistry?: ComponentRegistry;
  enabled: boolean;
}

export function createIntentHandlers(deps: IntentHandlerDeps) {
  return {
    'intents.list': async (request: GatewayRequest): Promise<OutboundMessage> => {
      const rules = deps.intentRegistry?.list() ?? [];
      return makeResponse(request.id, {
        enabled: deps.enabled,
        rules,
      });
    },

    'intents.match': async (request: GatewayRequest): Promise<OutboundMessage> => {
      const params = request.params as { input?: string } | undefined;
      if (!params?.input) {
        return makeError(request.id, ErrorCode.InvalidRequest, 'input is required');
      }

      const match = deps.intentRegistry?.match(params.input) ?? null;
      return makeResponse(request.id, {
        enabled: deps.enabled,
        match,
      });
    },
  };
}
