import { REST, Routes } from "discord.js";
import { env } from "../core/env.js";
import { logger } from "../core/logger.js";
import { commands } from "../commands/index.js";

export async function registerCommands() {
  const rest = new REST({ version: "10" }).setToken(env.DISCORD_TOKEN);
  const body = commands.map((c) => c.data.toJSON());

  if (env.DISCORD_GUILD_ID) {
    await rest.put(
      Routes.applicationGuildCommands(env.DISCORD_CLIENT_ID, env.DISCORD_GUILD_ID),
      { body },
    );
    logger.info({ guildId: env.DISCORD_GUILD_ID, count: body.length }, "Guild commands registered");
    return;
  }

  await rest.put(Routes.applicationCommands(env.DISCORD_CLIENT_ID), { body });
  logger.info({ count: body.length }, "Global commands registered");
}
