import { EmbedBuilder } from "discord.js";

/**
 * Quest entry with quest details and progress
 */
interface QuestEntry {
    progress: number | null;
    completedAt: Date | null;
    quest: {
        name: string;
        description: string | null;
        rewards: any;
    };
}

/**
 * Formats quest rewards object into a human-readable string
 */
function formatQuestRewards(rewards: { xp?: number, balance?: number }): string {
    const rewardStr: string[] = [];
    if (rewards?.xp) rewardStr.push(`${rewards.xp} XP`);
    if (rewards?.balance) rewardStr.push(`${rewards.balance} 🪙`);
    return rewardStr.join(", ");
}

/**
 * Returns the quest status display string
 */
function getQuestStatus(completedAt: Date | null): string {
    return completedAt ? "✅ Completed" : "📝 In Progress";
}

/**
 * Creates an embed displaying a user's quest log
 */
export function getQuestListEmbed(userQuests: QuestEntry[]): EmbedBuilder {
    const embed = new EmbedBuilder()
        .setTitle("📜 Quest Log")
        .setColor(0x3498db); // Blue

    userQuests.forEach(entry => {
        const status = getQuestStatus(entry.completedAt);
        const rewards = entry.quest.rewards as { xp?: number, balance?: number };
        const rewardsText = formatQuestRewards(rewards);

        embed.addFields({
            name: `${entry.quest.name} (${status})`,
            value: `${entry.quest.description}\n**Rewards:** ${rewardsText}\n**Progress:** ${entry.progress}%`,
            inline: false
        });
    });

    return embed;
}
