import { getCommandMentionByName } from "./../utils/getCommandDataByName.js";
import type { NucleusClient } from "../utils/client.js";
import convertCurlyBracketString from "../utils/convertCurlyBracketString.js";
import client from "../utils/client.js";
import EmojiEmbed from "../utils/generateEmojiEmbed.js";
import { GuildChannel, GuildMember, BaseGuildTextChannel } from "discord.js";
import singleNotify from "../utils/singleNotify.js";

export async function callback(_client: NucleusClient, member: GuildMember) {
    if (member.user.bot) return;
    const config = await client.database.guilds.read(member.guild.id);
    if (!config.welcome.enabled) return;

    if (config.welcome.channel) {
        let string = config.welcome.message;
        if (string) {
            string = await convertCurlyBracketString(
                string,
                member.id,
                member.displayName,
                member.guild.name,
                member.guild.members
            );
            if (config.welcome.channel === "dm") {
                await member.send({
                    embeds: [new EmojiEmbed().setDescription(string).setStatus("Success")]
                });
            } else {
                const channel: GuildChannel | null = (await member.guild.channels.fetch(
                    config.welcome.channel
                )) as GuildChannel | null;
                if (!channel)
                    return await singleNotify(
                        "welcomeChannelDeleted",
                        member.guild.id,
                        `The welcome channel has been deleted or is no longer accessible. Use ${getCommandMentionByName(
                            "settings/welcome"
                        )} to set a new one`,
                        "Warning"
                    );
                if (!(channel instanceof BaseGuildTextChannel)) return;
                if (channel.guild.id !== member.guild.id) return;
                try {
                    await channel.send({
                        embeds: [new EmojiEmbed().setDescription(string).setStatus("Success")],
                        content: (config.welcome.ping ? `<@&${config.welcome.ping}>` : "") + `<@${member.id}>`
                    });
                } catch (err) {
                    singleNotify(
                        "welcomeChannelDeleted",
                        member.guild.id,
                        `The welcome channel has been deleted or is no longer accessible. Use ${getCommandMentionByName(
                            "settings/welcome"
                        )} to set a new one`,
                        "Warning"
                    );
                }
            }
        }
    }
}
