/**
 * spawn_bots.js -- Spawn multiple Mineflayer bots on the dev server.
 *
 * Each bot connects, reports its state, and stays connected for
 * external control (RCON commands, chat interaction, etc.).
 *
 * Usage: node spawn_bots.js [count] [host] [port]
 * Defaults: 3 bots, 192.168.0.244:25568
 *
 * Bots are named: TrainBot_0, TrainBot_1, TrainBot_2, ...
 * They join staggered (1s apart) to avoid connection floods.
 */

const mineflayer = require('mineflayer');

const count = parseInt(process.argv[2] || '3', 10);
const host = process.argv[3] || '192.168.0.244';
const port = parseInt(process.argv[4] || '25568', 10);

const bots = [];
let connected = 0;

function spawnBot(index) {
  const name = `TrainBot_${index}`;
  console.log(`[${name}] Connecting to ${host}:${port}...`);

  const bot = mineflayer.createBot({
    host,
    port,
    username: name,
    auth: 'offline',
    version: '1.21.11',
  });

  bot._botIndex = index;
  bot._botName = name;
  bots.push(bot);

  bot.on('login', () => {
    console.log(`[${name}] Logged in`);
  });

  bot.on('spawn', () => {
    connected++;
    const pos = bot.entity.position;
    console.log(`[${name}] Spawned at (${pos.x.toFixed(0)}, ${pos.y.toFixed(0)}, ${pos.z.toFixed(0)}) -- ${connected}/${count} connected`);

    if (connected === count) {
      console.log(`\n=== All ${count} bots connected ===`);
      console.log('Bots are idle and ready for training commands.');
      console.log('Press Ctrl+C to disconnect all bots.\n');
    }
  });

  bot.on('chat', (username, message) => {
    // Ignore own messages
    if (username === name) return;
    console.log(`[${name}] CHAT <${username}> ${message}`);
  });

  bot.on('error', (err) => {
    console.error(`[${name}] ERROR: ${err.message}`);
  });

  bot.on('kicked', (reason) => {
    console.error(`[${name}] KICKED: ${reason}`);
    connected = Math.max(0, connected - 1);
  });

  bot.on('end', (reason) => {
    console.log(`[${name}] Disconnected: ${reason}`);
    connected = Math.max(0, connected - 1);
  });

  return bot;
}

// Stagger connections
for (let i = 0; i < count; i++) {
  setTimeout(() => spawnBot(i), i * 1500);
}

// Graceful shutdown
process.on('SIGINT', () => {
  console.log('\nDisconnecting all bots...');
  for (const bot of bots) {
    try { bot.quit('Shutdown'); } catch (_) {}
  }
  setTimeout(() => process.exit(0), 2000);
});

// Keep alive
setInterval(() => {}, 60000);
