import { createCommand } from "@shared/lib/utils";
import { SlashCommandBuilder, AttachmentBuilder } from "discord.js";
import { userService } from "@shared/modules/user/user.service";
import { generateStudentIdCard } from "@/graphics/studentID";
import { createWarningEmbed } from "@/lib/embeds";

export const profile = createCommand({
    data: new SlashCommandBuilder()
        .setName("profile")
        .setDescription("View your or another user's profile")
        .addUserOption(option =>
            option.setName("user")
                .setDescription("The user to view")
                .setRequired(false)
        ),
    execute: async (interaction) => {
        await interaction.deferReply();

        const targetUser = interaction.options.getUser("user") || interaction.user;

        if (targetUser.bot) {
            await interaction.editReply({ embeds: [createWarningEmbed("Bots do not have profiles.", "Profile Check")] });
            return;
        }

        const user = await userService.getOrCreateUser(targetUser.id, targetUser.username);

        const cardBuffer = await generateStudentIdCard({
            username: targetUser.username,
            avatarUrl: targetUser.displayAvatarURL({ extension: 'png', size: 256 }),
            id: targetUser.id,
            level: user!.level || 1,
            xp: user!.xp || 0n,
            au: user!.balance || 0n,
            className: user!.class?.name || "D"
        });

        const attachment = new AttachmentBuilder(cardBuffer, { name: 'student-id.png' });

        await interaction.editReply({ files: [attachment] });

    }
});
