import { getCommandMentionByName } from './../utils/getCommandMentionByName.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 ${await 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 ${await getCommandMentionByName("settings/welcome")} to set a new one`,
                        "Warning"
                    )
                }
            }
        }
    }
}
