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

export const moderationCase = createCommand({
    data: new SlashCommandBuilder()
        .setName("case")
        .setDescription("View details of a specific moderation case")
        .addStringOption(option =>
            option
                .setName("case_id")
                .setDescription("The case ID (e.g., CASE-0001)")
                .setRequired(true)
        )
        .setDefaultMemberPermissions(PermissionFlagsBits.Administrator),

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

        try {
            const caseId = interaction.options.getString("case_id", true).toUpperCase();

            // Validate case ID format
            if (!caseId.match(/^CASE-\d+$/)) {
                await interaction.editReply({
                    embeds: [getModerationErrorEmbed("Invalid case ID format. Expected format: CASE-0001")]
                });
                return;
            }

            // Get the case
            const moderationCase = await ModerationService.getCaseById(caseId);

            if (!moderationCase) {
                await interaction.editReply({
                    embeds: [getModerationErrorEmbed(`Case **${caseId}** not found.`)]
                });
                return;
            }

            // Display the case
            await interaction.editReply({
                embeds: [getCaseEmbed(moderationCase)]
            });

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