import { Client, GatewayIntentBits, Partials, Collection } from 'discord.js';
import { logger } from '../utils/logger';
import { env } from '../config/env';
import { loadCommands } from '../handlers/CommandLoader';
import { loadEvents } from '../handlers/EventLoader';
import { handleGlobalExceptions } from '../utils/errorHandler';
import { connectDB } from '../database';
import { FeverService } from '../services/FeverService';


export class KordClient extends Client {
  public commands: Collection<string, any> = new Collection();

  constructor() {
    super({
      intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildVoiceStates,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.MessageContent,
        GatewayIntentBits.GuildMembers,
        GatewayIntentBits.GuildInvites,
      ],

      partials: [Partials.Message, Partials.Channel, Partials.GuildMember],
    });
  }

  public async start() {
    handleGlobalExceptions();
    
    // Connect to external services
    await connectDB();

    // Load Handlers
    await loadCommands(this);
    await loadEvents(this);

    if (!env.DISCORD_TOKEN) {
      logger.warn('DISCORD_TOKEN is missing. Bot cannot start.');
      return;
    }

    // Login
    await this.login(env.DISCORD_TOKEN);
    logger.info(`Started login sequence...`);
  }
}
