import type { AdapterTemplate } from '../types';

/**
 * Distributed adapter - multi-pod Node.js deployment with HA.
 * Compiles to CommonJS with an HA setup file that sets FRONTMCP_DEPLOYMENT_MODE.
 *
 * The setup file runs before any imports to ensure the deployment mode
 * is detected before @FrontMcp decorators execute. This enables:
 * - Session heartbeat and takeover
 * - Cross-pod notification relay via Redis pub/sub
 * - LB affinity via __frontmcp_node cookie
 * - Machine ID from K8s HOSTNAME or os.hostname()
 *
 * Requires Redis configuration in @FrontMcp() for session persistence.
 */
export const distributedAdapter: AdapterTemplate = {
  moduleFormat: 'commonjs',

  getSetupTemplate: () => `// Distributed HA setup - MUST be imported first
// Sets FRONTMCP_DEPLOYMENT_MODE before any decorators run
// Enables heartbeat, session takeover, and notification relay
process.env.FRONTMCP_DEPLOYMENT_MODE = 'distributed';
`,

  getEntryTemplate: (mainModulePath: string) => `// Auto-generated distributed entry point
// Generated by: frontmcp build --target distributed
// The build emits the HA bootstrap as serverless-setup.js (shared name across
// all serverless/distributed adapters); requiring ha-setup.js used to crash at
// runtime because that filename was never written.
require('./serverless-setup.js');
require('${mainModulePath}');
`,
};
