const fs = require('fs');
const path = require('path');

const vendorDir = path.join(__dirname, '../vendor');
const activeSkillsDir = path.join(__dirname, '../skills');

const targets = [
    { src: path.join(vendorDir, 'superpowers/skills'), type: 'directory_of_skills' },
    { src: path.join(vendorDir, 'obsidian-skills/skills'), type: 'directory_of_skills' }
];

let copiedCount = 0;

if (!fs.existsSync(activeSkillsDir)) {
    fs.mkdirSync(activeSkillsDir, { recursive: true });
}

targets.forEach(target => {
    if (fs.existsSync(target.src)) {
        const items = fs.readdirSync(target.src);
        items.forEach(item => {
            const srcPath = path.join(target.src, item);
            const destPath = path.join(activeSkillsDir, item);
            
            if (fs.statSync(srcPath).isDirectory()) {
                // Ensure SKILL.md exists before copying
                if (fs.existsSync(path.join(srcPath, 'SKILL.md'))) {
                    // Node 16.7+ standard copy
                    fs.cpSync(srcPath, destPath, { recursive: true, force: true });
                    copiedCount++;
                }
            }
        });
    }
});

// Create .gitignore to avoid committing flattened skills to the root repo
// This keeps the Zero-Pollution architecture intact!
const gitignorePath = path.join(activeSkillsDir, '.gitignore');
const gitignoreContent = [
    '# Auto-generated by extract_skills.js',
    '*',                 // Ignore everything by default
    '!.gitignore',       // Track this file
    '!ui-ux-pro-max/',   // Keep manually copied ones
    '!browser_use/',     // Keep if explicitly placed
    '!mini-swe/',
    '!gsd-*/'            // Essential for tracking auto-generated GSD commands
].join('\n');

fs.writeFileSync(gitignorePath, gitignoreContent, 'utf8');

console.log(`Successfully extracted and flattened ${copiedCount} internal skills to .agent/skills/`);
