import { commitinfo } from './00_commitinfo_data.js';
export * from './00_commitinfo_data.js';

// Re-export smartmta (excluding commitinfo to avoid naming conflict)
export { UnifiedEmailServer } from '@push.rocks/smartmta';
export type { IUnifiedEmailServerOptions, IEmailRoute, IEmailDomainConfig } from '@push.rocks/smartmta';

// DcRouter
import { DcRouter } from './classes.dcrouter.js';
export * from './classes.dcrouter.js';

// RADIUS module
export * from './radius/index.js';

// Remote Ingress module
export * from './remoteingress/index.js';

// HTTP/3 module
export type { IHttp3Config } from './http3/index.js';

export const runCli = async () => {
  const args = process.argv.slice(2);

  if (args.includes('--version') || args.includes('version')) {
    console.log(commitinfo.version);
    return;
  }

  if (args.includes('--help') || args.includes('-h') || args.includes('help')) {
    console.log(`dcrouter ${commitinfo.version}

Usage:
  dcrouter
  dcrouter --version
  dcrouter --help

Environment:
  DCROUTER_MODE=OCI_CONTAINER   Start with OCI container configuration
  DCROUTER_DNS_BIND_INTERFACE   Override the embedded DNS UDP bind address
  DATA_DIR=<path>               Override the writable dcrouter data directory
`);
    return;
  }

  let options: import('./classes.dcrouter.js').IDcRouterOptions = {};

  if (process.env.DCROUTER_MODE === 'OCI_CONTAINER') {
    const { getOciContainerConfig } = await import('../ts_oci_container/index.js');
    options = getOciContainerConfig();
    console.log('[DCRouter] Starting in OCI Container mode...');
  }

  const dcRouter = new DcRouter(options);
  await dcRouter.start();
  console.log('[DCRouter] Running. Send SIGTERM or SIGINT to stop.');

  const shutdown = async () => {
    console.log('[DCRouter] Shutting down...');
    await dcRouter.stop();
    process.exit(0);
  };
  process.once('SIGINT', shutdown);
  process.once('SIGTERM', shutdown);
};
