import { EmbedBuilder } from "discord.js";

/**
 * User data for leaderboard display
 */
interface LeaderboardUser {
    username: string;
    level: number | null;
    xp: bigint | null;
    balance: bigint | null;
    netWorth?: bigint | null;
}

/**
 * Returns the appropriate medal emoji for a ranking position
 */
function getMedalEmoji(index: number): string {
    if (index === 0) return "🥇";
    if (index === 1) return "🥈";
    if (index === 2) return "🥉";
    return `${index + 1}.`;
}

/**
 * Formats a single leaderboard entry based on type
 */
function formatLeaderEntry(user: LeaderboardUser, index: number, type: 'xp' | 'balance' | 'networth'): string {
    const medal = getMedalEmoji(index);
    let value = '';

    switch (type) {
        case 'xp':
            value = `Lvl ${user.level ?? 1} (${user.xp ?? 0n} XP)`;
            break;
        case 'balance':
            value = `${user.balance ?? 0n} 🪙`;
            break;
        case 'networth':
            value = `${user.netWorth ?? 0n} 🪙 (Net Worth)`;
            break;
    }

    return `${medal} **${user.username}** — ${value}`;
}

/**
 * Creates a leaderboard embed for either XP, Balance or Net Worth rankings
 */
export function getLeaderboardEmbed(leaders: LeaderboardUser[], type: 'xp' | 'balance' | 'networth'): EmbedBuilder {
    const description = leaders.map((user, index) =>
        formatLeaderEntry(user, index, type)
    ).join("\n");

    let title = '';
    switch (type) {
        case 'xp':
            title = "🏆 XP Leaderboard";
            break;
        case 'balance':
            title = "💰 Richest Players";
            break;
        case 'networth':
            title = "💎 Net Worth Leaderboard";
            break;
    }

    return new EmbedBuilder()
        .setTitle(title)
        .setDescription(description)
        .setColor(0xFFD700); // Gold
}
