import { systemEvents, EVENTS } from "@shared/lib/events";
import { dashboardService } from "@shared/modules/dashboard/dashboard.service";

/**
 * Service to handle administrative actions triggered from the dashboard.
 * These actions are broadcasted to the bot via the system event bus.
 */
export const actionService = {
    /**
     * Triggers a reload of all bot commands.
     */
    reloadCommands: async () => {
        systemEvents.emit(EVENTS.ACTIONS.RELOAD_COMMANDS);

        await dashboardService.recordEvent({
            type: "info",
            message: "Admin: Triggered command reload",
            icon: "♻️"
        });

        return { success: true, message: "Command reload triggered" };
    },

    /**
     * Triggers a clearance of internal bot caches.
     */
    clearCache: async () => {
        systemEvents.emit(EVENTS.ACTIONS.CLEAR_CACHE);

        await dashboardService.recordEvent({
            type: "info",
            message: "Admin: Triggered cache clearance",
            icon: "🧹"
        });

        return { success: true, message: "Cache clearance triggered" };
    },

    /**
     * Toggles maintenance mode for the bot.
     */
    toggleMaintenanceMode: async (enabled: boolean, reason?: string) => {
        systemEvents.emit(EVENTS.ACTIONS.MAINTENANCE_MODE, { enabled, reason });

        await dashboardService.recordEvent({
            type: enabled ? "warn" : "info",
            message: `Admin: Maintenance mode ${enabled ? "ENABLED" : "DISABLED"}${reason ? ` (${reason})` : ""}`,
            icon: "🛠️"
        });

        return { success: true, enabled, message: `Maintenance mode ${enabled ? "enabled" : "disabled"}` };
    }
};
