import { KordClient } from '../client/KordClient';
import { logger } from '../utils/logger';
import fs from 'fs';
import path from 'path';

export const loadEvents = async (client: KordClient) => {
  const eventsPath = path.join(__dirname, '../events');
  if (!fs.existsSync(eventsPath)) return;

  const eventFiles = fs.readdirSync(eventsPath).filter(f => f.endsWith('.ts') || f.endsWith('.js'));
  
  for (const file of eventFiles) {
    const filePath = path.join(eventsPath, file);
    const event = require(filePath).default;
    if (!event || !event.name || !event.execute) {
      logger.warn(`Invalid event structure in ${file}`);
      continue;
    }
    
    if (event.once) {
      client.once(event.name, (...args) => event.execute(...args, client));
    } else {
      client.on(event.name, (...args) => event.execute(...args, client));
    }
    logger.debug(`Loaded event: ${event.name}`);
  }
};
