require('dotenv').config();
const { Framework } = require('../src');

async function main() {
  const framework = new Framework({ debug: true });

  await framework.bootstrap({
    agentDir: process.cwd() + '/.agent',
    aiConfig: {
      provider: process.env.FOLIKO_PROVIDER || 'minimax',
      model: process.env.FOLIKO_MODEL || 'MiniMax-M2.7',
      baseURL: process.env.FOLIKO_BASE_URL || 'https://api.minimaxi.com/v1',
      apiKey: process.env.MINIMAX_API_KEY || process.env.DEEPSEEK_API_KEY,
    },
  });

  // 检查 telegram 插件
  const tgPlugin = framework.pluginManager.get('telegram');

  if (!tgPlugin) {
    console.log('Telegram plugin NOT loaded!');
    await framework.destroy();
    return;
  }

  console.log('\n=== Telegram Plugin Debug ===');
  console.log('Bot token:', tgPlugin.config.botToken ? 'set' : 'NOT set');
  console.log('Bot instance:', tgPlugin._bot ? 'exists' : 'NOT exists');
  console.log('Group mode:', tgPlugin.config.groupMode);
  console.log('===========================\n');

  if (!tgPlugin._bot) {
    console.log('Bot is not initialized. Check if TELEGRAM_BOT_TOKEN is valid.');
    await framework.destroy();
    return;
  }

  // 监听所有事件
  tgPlugin._bot.on('message', (msg) => {
    console.log('\n[MESSAGE RECEIVED]');
    console.log('  chat.id:', msg.chat?.id);
    console.log('  chat.type:', msg.chat?.type);
    console.log('  from.username:', msg.from?.username);
    console.log('  text:', msg.text);
    console.log('===================\n');
  });

  tgPlugin._bot.on('polling_error', (err) => {
    console.log('\n[POLLING ERROR]', err.message);
  });

  tgPlugin._bot.on('error', (err) => {
    console.log('\n[BOT ERROR]', err.message);
  });

  console.log('Waiting for messages... Send a message to your bot.\n');

  // 保持运行
  await new Promise(() => {});
}

main().catch(console.error);
