import { createCommand } from "@shared/lib/utils";
import { SlashCommandBuilder, PermissionFlagsBits } from "discord.js";
import { moderationService } from "@shared/modules/moderation/moderation.service";
import { getClearSuccessEmbed, getModerationErrorEmbed } from "@/modules/moderation/moderation.view";
import { withCommandErrorHandling } from "@lib/commandUtils";

export const clearwarning = createCommand({
    data: new SlashCommandBuilder()
        .setName("clearwarning")
        .setDescription("Clear/resolve a warning")
        .addStringOption(option =>
            option
                .setName("case_id")
                .setDescription("The case ID to clear (e.g., CASE-0001)")
                .setRequired(true)
        )
        .addStringOption(option =>
            option
                .setName("reason")
                .setDescription("Reason for clearing the warning")
                .setRequired(false)
                .setMaxLength(500)
        )
        .setDefaultMemberPermissions(PermissionFlagsBits.Administrator),

    execute: async (interaction) => {
        await withCommandErrorHandling(
            interaction,
            async () => {
                const caseId = interaction.options.getString("case_id", true).toUpperCase();
                const reason = interaction.options.getString("reason") || "Cleared by moderator";

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

                // Check if case exists and is active
                const existingCase = await moderationService.getCaseById(caseId);

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

                if (!existingCase.active) {
                    await interaction.editReply({
                        embeds: [getModerationErrorEmbed(`Case **${caseId}** is already resolved.`)]
                    });
                    return;
                }

                if (existingCase.type !== 'warn') {
                    await interaction.editReply({
                        embeds: [getModerationErrorEmbed(`Case **${caseId}** is not a warning. Only warnings can be cleared.`)]
                    });
                    return;
                }

                // Clear the warning
                await moderationService.clearCase({
                    caseId,
                    clearedBy: interaction.user.id,
                    clearedByName: interaction.user.username,
                    reason
                });

                // Send success message
                await interaction.editReply({
                    embeds: [getClearSuccessEmbed(caseId)]
                });
            },
            { ephemeral: true }
        );
    }
});
