const { Client, GatewayIntentBits, EmbedBuilder } = require('discord.js');
const axios = require('axios');
// Load configuration from environment variables or config.json
const config = {
    token: process.env.DISCORD_TOKEN || require('./config.json').token,
    guildID: process.env.GUILD_ID || require('./config.json').guildID,
    channelID: process.env.CHANNEL_ID || require('./config.json').channelID,
    clientID: process.env.CLIENT_ID || require('./config.json').clientID,
    updatetime: parseInt(process.env.UPDATE_TIME) || require('./config.json').updatetime,
    backendUrl: process.env.BACKEND_URL || '<YOUR_BACKEND_URL>',
    uptimeKumaUrl: process.env.UPTIME_KUMA_URL || '<YOUR_UPTIMEKUMA_URL>'
};

const client = new Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.MessageContent
    ]
});

let monitorMessages = {
    Gaming: null,
    Discord: null,
    Web: null
};

client.once('ready', async () => {
    console.log('Bot is online!');
    
    const channel = await client.channels.fetch(config.channelID);
if (channel && channel.isTextBased()) {
    await clearChannel(channel);
} else {
    console.error(`Unable to find text channel with ID ${config.channelID}`);
}
    // Call the function to update messages immediately
    await updateMessages();
    // Set interval to update messages every 30 seconds
    setInterval(updateMessages, config.updatetime * 1000);
});

async function updateMessages() {
    try {
        const guild = await client.guilds.fetch(config.guildID);
        if (!guild) {
            console.error(`Unable to find guild with ID ${config.guildID}`);
            return;
        }

        const channel = await guild.channels.fetch(config.channelID);
        if (!channel || !channel.isTextBased()) {
            console.error(`Unable to find text channel with ID ${config.channelID}`);
            return;
        }

        const response = await axios.get(config.backendUrl);
        const monitors = response.data;

        const gamingMonitors = monitors.filter(monitor => [
           'Lobby', 'Skyblock', 'Survival', 'Creative', 'KitPvP', 'Factions', 'Prison', 'Skywars'
        ].includes(monitor.monitor_name));

        const discordMonitors = monitors.filter(monitor => [
            'Discord bot', 'Status bot'
        ].includes(monitor.monitor_name));

        const webMonitors = monitors.filter(monitor => [
            'web1', 'web2', 'web3'
        ].includes(monitor.monitor_name));

        await sendMonitorsMessage(channel, 'Gaming', gamingMonitors);
        await sendMonitorsMessage(channel, 'Discord', discordMonitors);
        await sendMonitorsMessage(channel, 'Web', webMonitors);

    } catch (error) {
        console.error('Error:', error);
    }
}

async function sendMonitorsMessage(channel, category, monitors) {
    let description = monitors.map(monitor => {
        let statusEmoji = '';
        switch (monitor.status) {
            case 0:
                statusEmoji = '🔴'; // Offline
                break;
            case 1:
                statusEmoji = '🟢'; // Online
                break;
            case 2:
                statusEmoji = '🟡'; // Warning
                break;
            case 3:
                statusEmoji = '🔵'; // Maintenance
                break;
            default:
                statusEmoji = '❓'; // Unknown
        }
        return `${statusEmoji} | ${monitor.monitor_name}`;
    }).join('\n');

    let embed = new EmbedBuilder()
        .setTitle(`${category} Monitor`)
        .setColor('#0099ff')
        .setDescription(description)
        .setFooter({ text: `Last updated: ${new Date().toLocaleString()}` })
        .setURL(config.uptimeKumaUrl);

    try {
        
        if (monitorMessages[category]) {
            const message = await channel.messages.fetch(monitorMessages[category]);
            if (message) {
                await message.edit({ embeds: [embed] });
                console.log(`${new Date().toLocaleString()} | Updated ${category} monitors message`);
            } else {
                const newMessage = await channel.send({ embeds: [embed] });
                monitorMessages[category] = newMessage.id;
                console.log(`${new Date().toLocaleString()} | Sent new ${category} monitors message`);
            }
        } else {
            const newMessage = await channel.send({ embeds: [embed] });
            monitorMessages[category] = newMessage.id;
            console.log(`${new Date().toLocaleString()} | Sent ${category} monitors message`);
        }
    } catch (error) {
        console.error(`Failed to send/update ${category} monitors message:`, error);
    }
}

async function clearChannel(channel) {
    try {
        const fetchedMessages = await channel.messages.fetch();
        await channel.bulkDelete(fetchedMessages);
        console.log('Cleared channel');
    } catch (error) {
        console.error('Error clearing channel:', error);
    }
}
client.login(config.token).catch(error => {
    console.error('Error logging in:', error);
});