import { createCommand } from "@shared/lib/utils";
import { SlashCommandBuilder } from "discord.js";
import { inventoryService } from "@shared/modules/inventory/inventory.service";
import { userService } from "@shared/modules/user/user.service";
import { createErrorEmbed } from "@lib/embeds";
import { getItemUseResultEmbed } from "@/modules/inventory/inventory.view";
import { withCommandErrorHandling } from "@lib/commandUtils";
import { getGuildConfig } from "@shared/lib/config";

export const use = createCommand({
    data: new SlashCommandBuilder()
        .setName("use")
        .setDescription("Use an item from your inventory")
        .addNumberOption(option =>
            option.setName("item")
                .setDescription("The item to use")
                .setRequired(true)
                .setAutocomplete(true)
        ),
    execute: async (interaction) => {
        await withCommandErrorHandling(
            interaction,
            async () => {
                const guildConfig = await getGuildConfig(interaction.guildId!);
                const colorRoles = guildConfig.colorRoles ?? [];

                const itemId = interaction.options.getNumber("item", true);
                const user = await userService.getOrCreateUser(interaction.user.id, interaction.user.username);
                if (!user) {
                    await interaction.editReply({ embeds: [createErrorEmbed("Failed to load user data.")] });
                    return;
                }

                const result = await inventoryService.useItem(user.id.toString(), itemId);

                const usageData = result.usageData;
                if (usageData) {
                    for (const effect of usageData.effects) {
                        if (effect.type === 'TEMP_ROLE' || effect.type === 'COLOR_ROLE') {
                            try {
                                const member = await interaction.guild?.members.fetch(user.id.toString());
                                if (member) {
                                    if (effect.type === 'TEMP_ROLE') {
                                        await member.roles.add(effect.roleId);
                                    } else if (effect.type === 'COLOR_ROLE') {
                                        // Remove existing color roles
                                        const rolesToRemove = colorRoles.filter(r => member.roles.cache.has(r));
                                        if (rolesToRemove.length > 0) await member.roles.remove(rolesToRemove);
                                        await member.roles.add(effect.roleId);
                                    }
                                }
                            } catch (e) {
                                console.error("Failed to assign role in /use command:", e);
                                result.results.push("⚠️ Failed to assign role (Check bot permissions)");
                            }
                        }
                    }
                }

                const { embed, files } = getItemUseResultEmbed(result.results, result.item);

                await interaction.editReply({ embeds: [embed], files });
            }
        );
    },
    autocomplete: async (interaction) => {
        const focusedValue = interaction.options.getFocused();
        const userId = interaction.user.id;

        const results = await inventoryService.getAutocompleteItems(userId, focusedValue);

        await interaction.respond(results);
    }
});
