/**
 * @fileoverview Dashboard stats helper for Aurora API.
 * Provides the getFullDashboardStats function used by stats routes.
 */

import { logger } from "@shared/lib/logger";

/**
 * Fetches comprehensive dashboard statistics.
 * Aggregates data from multiple services with error isolation.
 * 
 * @returns Full dashboard stats object including bot info, user counts,
 * economy data, leaderboards, and system status.
 */
export async function getFullDashboardStats() {
    // Import services (dynamic to avoid circular deps)
    const { dashboardService } = await import("@shared/modules/dashboard/dashboard.service");
    const { lootdropService } = await import("@shared/modules/economy/lootdrop.service");
    const { getClientStats } = await import("../../../bot/lib/clientStats");

    // Fetch all data in parallel with error isolation
    const results = await Promise.allSettled([
        Promise.resolve(getClientStats()),
        dashboardService.getActiveUserCount(),
        dashboardService.getTotalUserCount(),
        dashboardService.getEconomyStats(),
        dashboardService.getRecentEvents(10),
        dashboardService.getTotalItems(),
        dashboardService.getActiveLootdrops(),
        dashboardService.getLeaderboards(),
        Promise.resolve(lootdropService.getLootdropState()),
    ]);

    // Helper to unwrap result or return default
    const unwrap = <T>(result: PromiseSettledResult<T>, defaultValue: T, name: string): T => {
        if (result.status === 'fulfilled') return result.value;
        logger.error("web", `Failed to fetch ${name}`, result.reason);
        return defaultValue;
    };

    const clientStats = unwrap(results[0], {
        bot: { name: 'Aurora', avatarUrl: null, status: null },
        guilds: 0,
        commandsRegistered: 0,
        commandsKnown: 0,
        cachedUsers: 0,
        ping: 0,
        uptime: 0,
        lastCommandTimestamp: null
    }, 'clientStats');

    const activeUsers = unwrap(results[1], 0, 'activeUsers');
    const totalUsers = unwrap(results[2], 0, 'totalUsers');
    const economyStats = unwrap(results[3], { totalWealth: 0n, avgLevel: 0, topStreak: 0 }, 'economyStats');
    const recentEvents = unwrap(results[4], [], 'recentEvents');
    const totalItems = unwrap(results[5], 0, 'totalItems');
    const activeLootdrops = unwrap(results[6], [], 'activeLootdrops');
    const leaderboards = unwrap(results[7], { topLevels: [], topWealth: [], topNetWorth: [] }, 'leaderboards');
    const lootdropState = unwrap(results[8], undefined, 'lootdropState');

    return {
        bot: clientStats.bot,
        guilds: { count: clientStats.guilds },
        users: { active: activeUsers, total: totalUsers },
        commands: {
            total: clientStats.commandsKnown,
            active: clientStats.commandsRegistered,
            disabled: clientStats.commandsKnown - clientStats.commandsRegistered
        },
        ping: { avg: clientStats.ping },
        economy: {
            totalWealth: economyStats.totalWealth.toString(),
            avgLevel: economyStats.avgLevel,
            topStreak: economyStats.topStreak,
            totalItems,
        },
        recentEvents: recentEvents.map(event => ({
            ...event,
            timestamp: event.timestamp instanceof Date ? event.timestamp.toISOString() : event.timestamp,
        })),
        activeLootdrops: activeLootdrops.map(drop => ({
            rewardAmount: drop.rewardAmount,
            currency: drop.currency,
            createdAt: drop.createdAt.toISOString(),
            expiresAt: drop.expiresAt ? drop.expiresAt.toISOString() : null,
            // Explicitly excluding channelId/messageId to prevent sniping
        })),
        lootdropState,
        leaderboards,
        uptime: clientStats.uptime,
        lastCommandTimestamp: clientStats.lastCommandTimestamp,
        maintenanceMode: (await import("../../../bot/lib/BotClient")).AuroraClient.maintenanceMode,
    };
}
