/**
 * @fileoverview Administrative action endpoints for Aurora API.
 * Provides endpoints for system administration tasks like cache clearing
 * and maintenance mode toggling.
 */

import type { RouteContext, RouteModule } from "./types";
import { jsonResponse, errorResponse, parseBody, withErrorHandling } from "./utils";
import { MaintenanceModeSchema } from "./schemas";

/**
 * Admin actions routes handler.
 * 
 * Endpoints:
 * - POST /api/actions/reload-commands - Reload bot slash commands
 * - POST /api/actions/clear-cache - Clear internal caches
 * - POST /api/actions/maintenance-mode - Toggle maintenance mode
 */
async function handler(ctx: RouteContext): Promise<Response | null> {
    const { pathname, method, req } = ctx;

    // Only handle POST requests to /api/actions/*
    if (!pathname.startsWith("/api/actions/") || method !== "POST") {
        return null;
    }

    const { actionService } = await import("@shared/modules/admin/action.service");

    /**
     * @route POST /api/actions/reload-commands
     * @description Triggers a reload of all Discord slash commands.
     * Useful after modifying command configurations.
     * @response 200 - `{ success: true, message: string }`
     * @response 500 - Error reloading commands
     * 
     * @example
     * // Request
     * POST /api/actions/reload-commands
     * 
     * // Response
     * { "success": true, "message": "Commands reloaded" }
     */
    if (pathname === "/api/actions/reload-commands") {
        return withErrorHandling(async () => {
            const result = await actionService.reloadCommands();
            return jsonResponse(result);
        }, "reload commands");
    }

    /**
     * @route POST /api/actions/clear-cache
     * @description Clears all internal application caches.
     * Useful for forcing fresh data fetches.
     * @response 200 - `{ success: true, message: string }`
     * @response 500 - Error clearing cache
     * 
     * @example
     * // Request
     * POST /api/actions/clear-cache
     * 
     * // Response
     * { "success": true, "message": "Cache cleared" }
     */
    if (pathname === "/api/actions/clear-cache") {
        return withErrorHandling(async () => {
            const result = await actionService.clearCache();
            return jsonResponse(result);
        }, "clear cache");
    }

    /**
     * @route POST /api/actions/maintenance-mode
     * @description Toggles bot maintenance mode on or off.
     * When enabled, the bot will respond with a maintenance message.
     * 
     * @body { enabled: boolean, reason?: string }
     * @response 200 - `{ success: true, enabled: boolean }`
     * @response 400 - Invalid payload with validation errors
     * @response 500 - Error toggling maintenance mode
     * 
     * @example
     * // Request
     * POST /api/actions/maintenance-mode
     * Content-Type: application/json
     * { "enabled": true, "reason": "Deploying updates..." }
     * 
     * // Response
     * { "success": true, "enabled": true }
     */
    if (pathname === "/api/actions/maintenance-mode") {
        return withErrorHandling(async () => {
            const data = await parseBody(req, MaintenanceModeSchema);
            if (data instanceof Response) return data;

            const result = await actionService.toggleMaintenanceMode(data.enabled, data.reason);
            return jsonResponse(result);
        }, "toggle maintenance mode");
    }

    return null;
}

export const actionsRoutes: RouteModule = {
    name: "actions",
    handler
};
