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

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

  const commandFiles = fs.readdirSync(commandsPath).filter(f => f.endsWith('.ts') || f.endsWith('.js'));
  
  for (const file of commandFiles) {
    const filePath = path.join(commandsPath, file);
    try {
      const module = require(filePath);
      const command = module.default || module;
      
      if (command && 'data' in command && 'execute' in command) {
        client.commands.set(command.data.name, command);
        logger.debug(`Loaded command: ${command.data.name}`);
      } else {
        logger.warn(`The command at ${filePath} is missing a required "data" or "execute" property.`);
      }
    } catch (err) {
      logger.error(`Failed to load command at ${filePath}:`, err);
    }
  }
};
