import { createCommand } from "@/lib/utils";
import { SlashCommandBuilder, PermissionFlagsBits, MessageFlags } from "discord.js";
import { ModerationService } from "@/modules/moderation/moderation.service";
import { CaseType } from "@/lib/constants";
import { getNoteSuccessEmbed, getModerationErrorEmbed } from "@/modules/moderation/moderation.view";

export const note = createCommand({
    data: new SlashCommandBuilder()
        .setName("note")
        .setDescription("Add a staff-only note about a user")
        .addUserOption(option =>
            option
                .setName("user")
                .setDescription("The user to add a note for")
                .setRequired(true)
        )
        .addStringOption(option =>
            option
                .setName("note")
                .setDescription("The note to add")
                .setRequired(true)
                .setMaxLength(1000)
        )
        .setDefaultMemberPermissions(PermissionFlagsBits.Administrator),

    execute: async (interaction) => {
        await interaction.deferReply({ flags: MessageFlags.Ephemeral });

        try {
            const targetUser = interaction.options.getUser("user", true);
            const noteText = interaction.options.getString("note", true);

            // Create the note case
            const moderationCase = await ModerationService.createCase({
                type: CaseType.NOTE,
                userId: targetUser.id,
                username: targetUser.username,
                moderatorId: interaction.user.id,
                moderatorName: interaction.user.username,
                reason: noteText,
            });

            if (!moderationCase) {
                await interaction.editReply({
                    embeds: [getModerationErrorEmbed("Failed to create note.")]
                });
                return;
            }

            // Send success message
            await interaction.editReply({
                embeds: [getNoteSuccessEmbed(moderationCase.caseId, targetUser.username)]
            });

        } catch (error) {
            console.error("Note command error:", error);
            await interaction.editReply({
                embeds: [getModerationErrorEmbed("An error occurred while adding the note.")]
            });
        }
    }
});
