import fs from 'fs';
import path from 'path';
const SKILL_CATALOG = {
    'web-search': {
        description: 'Web search capability (built-in)',
        type: 'prompt',
        template: `---\nname: web-search\ndescription: "Search the web for information"\ntype: prompt\nversion: "1.0.0"\ncategories: [search, research]\n---\n\n# Web Search\n\nUse the built-in web_search tool to find information online.\n`,
    },
    'apify': {
        description: 'Web scraping via Apify Actors',
        type: 'mcp',
        template: `---\nname: apify\ndescription: "Web scraping via Apify Actors"\ntype: mcp\nversion: "1.0.0"\nmcp:\n  server_name: "apify"\n  command: "npx"\n  args: ["-y", "@apify/actors-mcp-server@latest"]\n  transport: "stdio"\nenv:\n  - APIFY_TOKEN\ncategories: [scraping, data]\n---\n\n# Apify\n\nUse Apify Actors for web scraping tasks.\n`,
    },
    'image-creator': {
        description: 'AI image generation via DALL-E or Stable Diffusion',
        type: 'mcp',
        template: `---\nname: image-creator\ndescription: "Generate images using AI models"\ntype: mcp\nversion: "1.0.0"\nmcp:\n  server_name: "image-creator"\n  command: "npx"\n  args: ["-y", "@modelcontextprotocol/server-dalle@latest"]\n  transport: "stdio"\nenv:\n  - OPENAI_API_KEY\ncategories: [design, content]\n---\n\n# Image Creator\n\nGenerate images using AI models for content creation.\n`,
    },
    'github': {
        description: 'GitHub integration for repositories, issues, PRs',
        type: 'mcp',
        template: `---\nname: github\ndescription: "GitHub integration for repository management"\ntype: mcp\nversion: "1.0.0"\nmcp:\n  server_name: "github"\n  command: "npx"\n  args: ["-y", "@modelcontextprotocol/server-github@latest"]\n  transport: "stdio"\nenv:\n  - GITHUB_TOKEN\ncategories: [development, integration]\n---\n\n# GitHub Integration\n\nManage GitHub repositories, issues, and pull requests.\n`,
    },
    'slack': {
        description: 'Slack messaging integration',
        type: 'mcp',
        template: `---\nname: slack\ndescription: "Send and receive Slack messages"\ntype: mcp\nversion: "1.0.0"\nmcp:\n  server_name: "slack"\n  command: "npx"\n  args: ["-y", "@anthropic/slack-mcp-server@latest"]\n  transport: "stdio"\nenv:\n  - SLACK_BOT_TOKEN\ncategories: [communication, integration]\n---\n\n# Slack Integration\n\nSend and receive messages via Slack.\n`,
    },
    'canva': {
        description: 'Design creation via Canva API',
        type: 'mcp',
        template: `---\nname: canva\ndescription: "Create designs using Canva"\ntype: mcp\nversion: "1.0.0"\nmcp:\n  server_name: "canva"\n  command: "npx"\n  args: ["-y", "@anthropic/canva-mcp-server@latest"]\n  transport: "stdio"\nenv:\n  - CANVA_API_KEY\ncategories: [design, content]\n---\n\n# Canva Integration\n\nCreate professional designs using Canva.\n`,
    },
};
export async function installCommand(skillName) {
    const cwd = process.cwd();
    const skillsDir = path.join(cwd, 'skills');
    if (!skillName) {
        console.log('\n  Available skills:\n');
        for (const [name, info] of Object.entries(SKILL_CATALOG)) {
            const installed = fs.existsSync(path.join(skillsDir, name));
            console.log(`  ${installed ? '[installed]' : '          '} ${name} — ${info.description}`);
        }
        console.log('\n  Usage: expxagents install <skill-name>\n');
        return;
    }
    const skill = SKILL_CATALOG[skillName];
    if (!skill) {
        console.error(`  Skill "${skillName}" not found in catalog.`);
        console.log('  Run `expxagents install` to see available skills.');
        process.exit(1);
    }
    const skillDir = path.join(skillsDir, skillName);
    if (fs.existsSync(skillDir)) {
        console.log(`  Skill "${skillName}" is already installed.`);
        return;
    }
    fs.mkdirSync(skillDir, { recursive: true });
    fs.writeFileSync(path.join(skillDir, 'SKILL.md'), skill.template, 'utf-8');
    console.log(`  Installed skill: ${skillName}`);
    if (skill.type === 'mcp') {
        console.log(`\n  This skill requires MCP configuration.`);
        console.log(`  Check .mcp.json and set required environment variables.`);
    }
}
