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

export const cases = createCommand({
    data: new SlashCommandBuilder()
        .setName("cases")
        .setDescription("View all moderation cases for a user")
        .addUserOption(option =>
            option
                .setName("user")
                .setDescription("The user to check cases for")
                .setRequired(true)
        )
        .addBooleanOption(option =>
            option
                .setName("active_only")
                .setDescription("Show only active cases (warnings)")
                .setRequired(false)
        )
        .setDefaultMemberPermissions(PermissionFlagsBits.Administrator),

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

        try {
            const targetUser = interaction.options.getUser("user", true);
            const activeOnly = interaction.options.getBoolean("active_only") || false;

            // Get cases for the user
            const userCases = await ModerationService.getUserCases(targetUser.id, activeOnly);

            const title = activeOnly
                ? `⚠️ Active Cases for ${targetUser.username}`
                : `📋 All Cases for ${targetUser.username}`;

            const description = userCases.length === 0
                ? undefined
                : `Total cases: **${userCases.length}**`;

            // Display the cases
            await interaction.editReply({
                embeds: [getCasesListEmbed(userCases, title, description)]
            });

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