require('dotenv').config();
const { Client, GatewayIntentBits } = require('discord.js');

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

    try {
        await client.login(process.env.BOT_TOKEN);
        
        console.log('\n=== Bot Setup Verification ===');
        console.log(`\nBot: ${client.user.tag}`);
        
        // Check servers
        console.log('\nServer Check:');
        if (client.guilds.cache.size === 0) {
            console.log('❌ Bot is not in any servers. Please use the invite link first.');
            return;
        }

        for (const guild of client.guilds.cache.values()) {
            console.log(`\nServer: ${guild.name}`);
            
            // Check bot's role
            const botRole = guild.members.cache.get(client.user.id)?.roles.highest;
            console.log('Role:', botRole ? botRole.name : '❌ No role found');
            
            // Check for notification channel
            const channel = guild.channels.cache.get(process.env.NOTIFICATION_CHANNEL_ID);
            if (channel) {
                console.log(`\n✅ Found notification channel: #${channel.name}`);
                
                // Check permissions
                const botMember = guild.members.cache.get(client.user.id);
                const perms = channel.permissionsFor(botMember);
                
                console.log('\nPermission Check:');
                console.log(`View Channel: ${perms.has('ViewChannel') ? '✅' : '❌'}`);
                console.log(`Send Messages: ${perms.has('SendMessages') ? '✅' : '❌'}`);
                console.log(`Embed Links: ${perms.has('EmbedLinks') ? '✅' : '❌'}`);
                console.log(`Attach Files: ${perms.has('AttachFiles') ? '✅' : '❌'}`);
                
                // Test message
                console.log('\nTesting channel access...');
                try {
                    const msg = await channel.send('🔍 Testing bot setup...');
                    await msg.delete();
                    console.log('✅ Successfully sent and deleted test message');
                } catch (error) {
                    console.log('❌ Failed to send test message:', error.message);
                }
            } else {
                console.log('\n❌ Notification channel not found in this server');
                console.log('Channel ID being checked:', process.env.NOTIFICATION_CHANNEL_ID);
            }
        }

        const channelFound = Array.from(client.guilds.cache.values()).some(
            guild => guild.channels.cache.has(process.env.NOTIFICATION_CHANNEL_ID)
        );

        if (!channelFound) {
            console.log('\n❌ The channel ID in your .env file was not found in any server');
            console.log('Current channel ID:', process.env.NOTIFICATION_CHANNEL_ID);
            console.log('\nTo fix this:');
            console.log('1. Right-click your desired notification channel');
            console.log('2. Click "Copy ID" (Enable Developer Mode if you don\'t see this)');
            console.log('3. Update NOTIFICATION_CHANNEL_ID in your .env file');
        }

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

// Call the async function properly
verifySetup().catch(error => {
    console.error('Setup verification failed:', error);
    process.exit(1);
});