import { logger } from '../utils/logger.js';
import path from 'path';
import fs from 'fs-extra';

export async function initSkills() {
  const skills = [];

  // Load built-in skills
  const skillsDir = path.join(process.cwd(), 'skills');
  
  if (await fs.pathExists(skillsDir)) {
    const skillFiles = await fs.readdir(skillsDir);
    
    for (const file of skillFiles) {
      if (file.endsWith('.js') || file.endsWith('.json')) {
        try {
          const skillPath = path.join(skillsDir, file);
          const skill = await fs.readJson(skillPath);
          
          skills.push({
            name: skill.name || file.replace(/\.(js|json)$/, ''),
            description: skill.description || '',
            version: skill.version || '1.0.0',
            category: skill.category || 'general',
          });
          
          logger.info(`✓ Loaded skill: ${skill.name}`);
        } catch (error) {
          logger.error(`✗ Failed to load skill ${file}:`, error.message);
        }
      }
    }
  }

  // Add built-in skills
  skills.push(
    {
      name: 'code_review',
      description: 'Review code for bugs and improvements',
      version: '1.0.0',
      category: 'development',
    },
    {
      name: 'bug_fix',
      description: 'Fix identified bugs in code',
      version: '1.0.0',
      category: 'development',
    },
    {
      name: 'refactor',
      description: 'Refactor code for better quality',
      version: '1.0.0',
      category: 'development',
    },
    {
      name: 'documentation',
      description: 'Generate and update documentation',
      version: '1.0.0',
      category: 'documentation',
    },
    {
      name: 'testing',
      description: 'Write tests for code',
      version: '1.0.0',
      category: 'testing',
    }
  );

  return skills;
}
