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

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 interaction.reply({ ephemeral: true, content: "Initializing terminal..." });

            try {
                await terminalService.init(channel as TextChannel);
                await interaction.editReply({ content: "✅ Terminal initialized!" });
            } catch (error) {
                console.error(error);
                await interaction.editReply({ content: "❌ Failed to initialize terminal." });
            }
        }
    }
});
