const { handleLinkCommand } = require('./commands');
const { handleCreateServerCommand } = require('./createserver');
const { handleDelServerCommand } = require('./delserver');
const { handleTasksCommand, handleTaskButton } = require('./tasks');
const discordRoleSync = require('../services/discordRoleSync');

function registerEvents(client) {
  client.on('interactionCreate', async interaction => {
    // Button interactions
    if (interaction.isButton()) {
      if (interaction.customId.startsWith('task_')) {
        await handleTaskButton(interaction);
      }
      return;
    }

    if (!interaction.isChatInputCommand()) return;
    if (interaction.commandName === 'link') {
      await handleLinkCommand(interaction);
    }
    if (interaction.commandName === 'createserver') {
      await handleCreateServerCommand(interaction);
    }
    if (interaction.commandName === 'delserver') {
      await handleDelServerCommand(interaction);
    }
    if (interaction.commandName === 'tasks') {
      await handleTasksCommand(interaction);
    }
  });

  client.on('ready', () => {
    console.log(`Discord bot logged in as ${client.user.tag}`);
    // Initialize role sync service with the ready client
    discordRoleSync.init(client);
  });
}

module.exports = { registerEvents };
