export async function setup(systemConfig, readline) {
  const question = (prompt) =>
    new Promise((resolve) => readline.question(prompt, resolve));

  console.log('\nTelegram Bridge Setup');
  console.log('─────────────────────────────────────────────');
  console.log('This bridge connects a Telegram bot to the Teletraan conductor agent.');
  console.log('You will need a bot token from @BotFather on Telegram.\n');

  const token = await question('Telegram Bot Token (from @BotFather): ');
  if (!token.trim()) {
    throw new Error('Telegram Bot Token is required');
  }

  console.log('\nAuthorized users: only these Telegram user IDs can message the bot.');
  console.log('Find your ID by messaging @userinfobot on Telegram.\n');

  const usersRaw = await question('Authorized Telegram user IDs (comma-separated): ');
  const authorizedUsers = usersRaw.split(',').map((s) => s.trim()).filter(Boolean);

  if (authorizedUsers.length === 0) {
    console.warn('Warning: no authorized users set — the bot will not respond to anyone.');
  }

  const defaultChannelUrl = systemConfig.ports?.conductor
    ? `http://127.0.0.1:${systemConfig.ports.conductor}`
    : 'http://127.0.0.1:7100';

  const channelUrlRaw = await question(`Conductor channel URL [${defaultChannelUrl}]: `);
  const channelUrl = channelUrlRaw.trim() || defaultChannelUrl;

  const channelSecret = await question('Channel auth secret (CHANNEL_SECRET from agent .env): ');

  console.log('\nTelegram bridge configured.');

  return {
    TELEGRAM_BOT_TOKEN: token.trim(),
    TELEGRAM_AUTHORIZED_USERS: authorizedUsers.join(','),
    CHANNEL_URL: channelUrl,
    CHANNEL_SECRET: channelSecret.trim(),
  };
}
