import type { DaemonConfig } from './server.js';

const required = (name: string): string => {
  const value = process.env[name];
  if (!value) throw new Error(`missing required env var ${name}`);
  return value;
};

export function configFromEnv(): DaemonConfig & { port: number } {
  return {
    homeserverUrl: required('MOSAIC_AS_HOMESERVER_URL'),
    domain: required('MOSAIC_AS_DOMAIN'),
    asToken: required('MOSAIC_AS_TOKEN'),
    hsToken: required('MOSAIC_HS_TOKEN'),
    userPrefix: process.env.MOSAIC_AS_USER_PREFIX ?? 'agent-',
    senderLocalpart: process.env.MOSAIC_AS_SENDER_LOCALPART ?? 'mosaic-as',
    bridgeTokens: (process.env.MOSAIC_AS_BRIDGE_TOKENS ?? '')
      .split(',')
      .map((t) => t.trim())
      .filter(Boolean),
    port: Number(process.env.MOSAIC_AS_PORT ?? 8008),
  };
}
