import { Module, type DynamicModule, type Provider } from '@nestjs/common';
import { McpRegistryService } from './mcp-registry.service';
import { McpTransportController } from './mcp-transport.controller';
import { MCP_MODULE_OPTIONS } from './mcp.constants';

export interface McpModuleOptions {
  aiServiceBaseUrl: string;
  /** Base URL for the NestJS API (e.g. http://localhost:3001/api/v1). Used by MCP servers that call NestJS endpoints instead of the AI service. */
  apiBaseUrl?: string;
  typesenseCollectionName?: string;
  /** When true, the built-in McpTransportController is NOT registered — useful when the host app provides its own authenticated controller. */
  skipDefaultController?: boolean;
}

export { MCP_MODULE_OPTIONS };

@Module({})
export class McpModule {
  static forRoot(options: McpModuleOptions): DynamicModule {
    const optionsProvider: Provider = {
      provide: MCP_MODULE_OPTIONS,
      useValue: options,
    };

    return {
      module: McpModule,
      controllers: options.skipDefaultController ? [] : [McpTransportController],
      providers: [optionsProvider, McpRegistryService],
      exports: [McpRegistryService],
      global: false,
    };
  }
}
