
import { createCommand } from "@shared/lib/utils";
import { SlashCommandBuilder } from "discord.js";
import { economyService } from "@shared/modules/economy/economy.service";
import { createErrorEmbed, createSuccessEmbed } from "@lib/embeds";
import { UserError } from "@shared/lib/errors";

export const daily = createCommand({
    data: new SlashCommandBuilder()
        .setName("daily")
        .setDescription("Claim your daily reward"),
    execute: async (interaction) => {
        await interaction.deferReply();
        try {
            const result = await economyService.claimDaily(interaction.user.id);

            const embed = createSuccessEmbed(`You claimed ** ${result.amount}** Astral Units!${result.isWeekly ? `\n🎉 **Weekly Bonus!** +${result.weeklyBonus} extra!` : ''}`, "💰 Daily Reward Claimed!")
                .addFields(
                    { name: "Streak", value: `🔥 ${result.streak} days`, inline: true },
                    { name: "Weekly Progress", value: `${"🟩".repeat(result.streak % 7 || 7)}${"⬜".repeat(7 - (result.streak % 7 || 7))} (${result.streak % 7 || 7}/7)`, inline: true },
                    { name: "Next Reward", value: `<t:${Math.floor(result.nextReadyAt.getTime() / 1000)}:R> `, inline: true }
                )
                .setColor("Gold");

            await interaction.editReply({ embeds: [embed] });

        } catch (error: any) {
            if (error instanceof UserError) {
                await interaction.editReply({ embeds: [createErrorEmbed(error.message)] });
            } else {
                console.error("Error claiming daily:", error);
                await interaction.editReply({ embeds: [createErrorEmbed("An unexpected error occurred.")] });
            }
        }
    }
});