module.exports = {
    name: "Edit Bot Guild Profile",

    description: "Edits the bot's profile for the specified guild",

    category: "Server Stuff",

    inputs: [
        {
            id: "action",
            name: "Action",
            description: "Executes this block",
            types: ["action"],
        },
        {
            id: "server",
            name: "Server",
            description: "The server to change the profile for",
            types: ["object", "unspecified"],
            required: true,
        },
        {
            id: "nickname",
            name: "Nickname",
            description: "New nickname. Use boolean false to reset.",
            types: ["text", "boolean", "unspecified"],
        },
        {
            id: "avatar",
            name: "Avatar",
            description: "New guild avatar (URL, base64, or Buffer). Use boolean false to reset.",
            types: ["text", "object", "boolean", "unspecified"],
        },
        {
            id: "banner",
            name: "Banner",
            description: "New guild banner (URL, base64, or Buffer). Use boolean false to reset.",
            types: ["text", "object", "boolean", "unspecified"],
        },
    ],

    options: [],

    outputs: [
        {
            id: "action",
            name: "Action",
            description: "Executes the following blocks when this block finishes its task.",
            types: ["action"],
        },
        {
            id: "member",
            name: "Member",
            description: "The updated member.",
            types: ["object", "unspecified"],
        },
        {
            id: "actionerror",
            name: "Action Error",
            description: "Executes the following blocks when this block fails.",
            types: ["action"],
        },
        {
            id: "error",
            name: "Error",
            description: "The error message if an error occurs.",
            types: ["text", "unspecified"],
        },
    ],

    async code(cache) {

        const server = this.GetInputValue("server", cache);
        let nickname = this.GetInputValue("nickname", cache);
        let avatar = this.GetInputValue("avatar", cache);
        let banner = this.GetInputValue("banner", cache);

        try {

            if (!server?.members?.editMe) {
                this.StoreOutputValue("Invalid server or editMe not available.", "error", cache);
                this.RunNextBlock("actionerror", cache);
                return;
            }

            const data = {};

            if (nickname !== undefined) {
                data.nick = nickname === false ? null : nickname;
            }

            if (avatar !== undefined) {
                data.avatar = avatar === false ? null : avatar;
            }

            if (banner !== undefined) {
                data.banner = banner === false ? null : banner;
            }

            const member = await server.members.editMe(data);

            this.StoreOutputValue(member, "member", cache);
            this.RunNextBlock("action", cache);

        } catch (err) {

            this.StoreOutputValue(err?.message ?? String(err), "error", cache);
            this.RunNextBlock("actionerror", cache);

        }
    },
};