
import { createCommand } from "@shared/lib/utils";
import { SlashCommandBuilder, PermissionFlagsBits, ChannelType, TextChannel } from "discord.js";
import { terminalService } from "@modules/system/terminal.service";
import { createErrorEmbed } from "@/lib/embeds";
import { withCommandErrorHandling } from "@lib/commandUtils";

export const terminal = createCommand({
    data: new SlashCommandBuilder()
        .setName("terminal")
        .setDescription("Manage the Aurora Terminal")
        .addSubcommand(sub =>
            sub.setName("init")
                .setDescription("Initialize the terminal in the current channel")
        )
        .setDefaultMemberPermissions(PermissionFlagsBits.Administrator),
    execute: async (interaction) => {
        const subcommand = interaction.options.getSubcommand();

        if (subcommand === "init") {
            const channel = interaction.channel;
            if (!channel || channel.type !== ChannelType.GuildText) {
                await interaction.reply({ embeds: [createErrorEmbed("Terminal can only be initialized in text channels.")] });
                return;
            }

            await withCommandErrorHandling(
                interaction,
                async () => {
                    await terminalService.init(channel as TextChannel);
                    await interaction.editReply({ content: "✅ Terminal initialized!" });
                },
                { ephemeral: true }
            );
        }
    }
});
