import { ChatInputCommandInteraction, MessageFlags } from "discord.js";
import { AuroraClient } from "@/lib/BotClient";
import { userService } from "@shared/modules/user/user.service";
import { featureFlagsService } from "@shared/modules/feature-flags/feature-flags.service";
import { createErrorEmbed } from "@lib/embeds";
import { logger } from "@shared/lib/logger";


/**
 * Handles slash command execution
 * Includes user validation and comprehensive error handling
 */
export class CommandHandler {
    static async handle(interaction: ChatInputCommandInteraction): Promise<void> {
        const command = AuroraClient.commands.get(interaction.commandName);

        if (!command) {
            logger.error("bot", `No command matching ${interaction.commandName} was found.`);
            return;
        }

        // Check maintenance mode
        if (AuroraClient.maintenanceMode) {
            const errorEmbed = createErrorEmbed('The bot is currently undergoing maintenance. Please try again later.');
            await interaction.reply({ embeds: [errorEmbed], flags: MessageFlags.Ephemeral });
            return;
        }

        // Check beta feature access
        if (command.beta) {
            const flagName = command.featureFlag || interaction.commandName;
            let memberRoles: string[] = [];
            
            if (interaction.member && 'roles' in interaction.member) {
                const roles = interaction.member.roles;
                if (typeof roles === 'object' && 'cache' in roles) {
                    memberRoles = [...roles.cache.keys()];
                } else if (Array.isArray(roles)) {
                    memberRoles = roles;
                }
            }
            
            const hasAccess = await featureFlagsService.hasAccess(flagName, {
                guildId: interaction.guildId!,
                userId: interaction.user.id,
                memberRoles,
            });

            if (!hasAccess) {
                const errorEmbed = createErrorEmbed(
                    "This feature is currently in beta testing and not available to all users. " +
                    "Stay tuned for the official release!",
                    "Beta Feature"
                );
                await interaction.reply({ embeds: [errorEmbed], flags: MessageFlags.Ephemeral });
                return;
            }
        }

        // Ensure user exists in database
        try {
            await userService.getOrCreateUser(interaction.user.id, interaction.user.username);
        } catch (error) {
            logger.error("bot", "Failed to ensure user exists", error);
        }

        try {
            await command.execute(interaction);
            AuroraClient.lastCommandTimestamp = Date.now();
        } catch (error) {
            logger.error("bot", `Error executing command ${interaction.commandName}`, error);
            const errorEmbed = createErrorEmbed('There was an error while executing this command!');

            if (interaction.replied || interaction.deferred) {
                await interaction.followUp({ embeds: [errorEmbed], flags: MessageFlags.Ephemeral });
            } else {
                await interaction.reply({ embeds: [errorEmbed], flags: MessageFlags.Ephemeral });
            }
        }
    }
}
