import convertCurlyBracketString from '../utils/convertCurlyBracketString.js'
import singleNotify from '../utils/singleNotify.js';
import client from '../utils/client.js';

interface PropSchema { enabled: boolean, name: string }

export async function callback(_, member) {
    let guild = await client.guilds.fetch(member.guild.id)
    let config = await client.database.guilds.read(guild.id);
    Object.entries(config.getKey("stats")).forEach(async ([channel, props]) => {
        if ((props as PropSchema).enabled) {
            let string = (props as PropSchema).name
            if (!string) return
            string = await convertCurlyBracketString(string, member.id, member.displayName, guild.name, guild.members)
            let fetchedChannel;
            try {
                fetchedChannel = await guild.channels.fetch(channel)
            } catch (e) { fetchedChannel = null }
            if (!fetchedChannel) {
                let deleted = config.getKey("stats")[channel]
                console.log(`stats.${channel}`)
                console.log(guild.id)
                await client.database.guilds.write(guild.id, null, `stats.${channel}`)
                return singleNotify(
                    "statsChannelDeleted",
                    guild.id,
                    "One or more of your stats channels have been deleted. Please use `/settings stats` if you wish to add the channel again.\n" +
                    `The channels name was: ${deleted.name}`,
                    "Critical"
                )
            }
            try {
                await fetchedChannel.setName(string.slice(0, 100))
            } catch (e) {
                console.error(e)
            }
        }
    });
}
