/**
 * 生成示例 Skills 到桌面（带分类目录 + DESCRIPTION.md + 索引缓存）
 *
 * 运行: npx tsx scripts/generate-example-skills.ts
 */

import * as fs from 'node:fs';
import * as path from 'node:path';
import * as os from 'node:os';
import { createSkillManageTool } from '../src/domain/skill/skill-manager-tool.js';
import { createSkillRegistry, loadSkillsWithCache, invalidateIndexCache } from '../src/domain/skill/skill-loader.js';

const outputDir = path.join(os.homedir(), 'Desktop', 'generated-skills');
fs.mkdirSync(outputDir, { recursive: true });
process.env.AGGROOT_HOME = outputDir;

const registry = createSkillRegistry();
const tool = createSkillManageTool(registry);

const skills: Array<{ name: string; category?: string; content: string }> = [
  // ---- devops 分类 ----
  {
    name: 'deploy-to-staging',
    category: 'devops',
    content: `---
name: deploy-to-staging
description: Deploy the project to staging environment using docker
whenToUse: When you need to deploy the current project to the staging server
---

# Deploy to Staging

1. Run tests: \`npm test\`
2. Build Docker image: \`docker build -t myapp:staging .\`
3. Push to registry: \`docker push registry.example.com/myapp:staging\`
4. SSH to staging server: \`ssh deploy@staging.example.com\`
5. Pull and restart: \`docker-compose pull && docker-compose up -d\`
6. Verify: \`curl -f https://staging.example.com/health\`

## Pitfalls
- Always run tests before deploying
- Check that .env.staging has the correct DATABASE_URL
- If health check fails, rollback with \`docker-compose down && docker-compose up -d --no-pull\`
`,
  },
  {
    name: 'rollback',
    category: 'devops',
    content: `---
name: rollback
description: Rollback a deployment to the previous version
whenToUse: When a deployment fails or causes issues in production
---

# Rollback Deployment

1. Check current version: \`docker-compose ps\`
2. Rollback to previous image: \`docker-compose down && docker-compose up -d --no-pull\`
3. Verify health: \`curl -f https://staging.example.com/health\`
4. Notify team in Slack

## Pitfalls
- Always verify the previous image is still available
- Don't rollback if database migration already ran
`,
  },
  // ---- debugging 分类 ----
  {
    name: 'debug-auth-401',
    category: 'debugging',
    content: `---
name: debug-auth-401
description: Debug and fix 401 authentication errors in test suites
whenToUse: When auth tests fail with 401 status code
---

# Debug Auth 401 Errors

1. Run the failing test with verbose output to identify which test and assertion failed
2. Read the auth middleware source code to understand the token validation logic
3. Check if the middleware correctly handles token formats (e.g., Bearer prefix stripping)
4. Fix the middleware to properly extract the token
5. Re-run tests to verify the fix

## Common Causes
- Middleware not stripping "Bearer " prefix from Authorization header
- Missing or incorrect token extraction logic
- Token not being passed in test fixtures
`,
  },
  {
    name: 'debug-memory-leak',
    category: 'debugging',
    content: `---
name: debug-memory-leak
description: Investigate and fix memory leaks in Node.js applications
whenToUse: When the application shows increasing memory usage over time
---

# Debug Memory Leak

1. Take heap snapshot: \`node --inspect app.js\` then Chrome DevTools → Memory → Take Snapshot
2. Compare snapshots over time to find growing objects
3. Check for common patterns: event listeners not removed, closures holding references, global caches
4. Fix the leak and verify with load testing

## Common Causes
- Event listeners not removed on cleanup
- Closures capturing large objects
- Unbounded caches without TTL
`,
  },
  // ---- git 分类 ----
  {
    name: 'smart-commit',
    category: 'git',
    content: `---
name: smart-commit
description: Create a well-structured git commit with proper message
whenToUse: When you need to commit changes to git
---

# Smart Commit

1. Run \`git status\` to see all changes
2. Run \`git diff\` to understand what changed
3. Stage appropriate files with \`git add\`
4. Create commit with conventional commit format:
   - feat: new feature
   - fix: bug fix
   - refactor: code refactoring

## Pitfalls
- Always review the diff before committing
- Never commit .env files or secrets
`,
  },
  // ---- 根级 skill（无分类）----
  {
    name: 'explain',
    content: `---
name: explain
description: Explain code or concept in detail
whenToUse: When you need to understand how something works
---

# Explain

1. Read the relevant source files
2. Trace the execution flow
3. Summarize the key concepts
4. Provide examples if helpful

Provide explanation at the requested detail level.
`,
  },
];

console.log('Generating skills with category structure...\n');

for (const skill of skills) {
  const result = await tool.execute({
    action: 'create',
    name: skill.name,
    content: skill.content,
    category: skill.category,
  });

  if (result.success) {
    console.log(`  ✓ ${skill.name}${skill.category ? ` (category: ${skill.category})` : ''}`);
  } else {
    console.log(`  ✗ ${skill.name}: ${result.stderr}`);
  }
}

// 手动写 DESCRIPTION.md 文件到分类目录
const skillsDir = path.join(outputDir, 'skills');
const descriptions: Record<string, string> = {
  devops: 'DevOps tools — deployment, rollback, and infrastructure management',
  debugging: 'Debugging workflows — systematic approaches to finding and fixing bugs',
  git: 'Git workflows — commit, branch, and version control best practices',
};

for (const [cat, desc] of Object.entries(descriptions)) {
  const descPath = path.join(skillsDir, cat, 'DESCRIPTION.md');
  if (fs.existsSync(path.join(skillsDir, cat))) {
    fs.writeFileSync(descPath, `---\ndescription: ${desc}\n---\n`, 'utf-8');
    console.log(`  ✓ ${cat}/DESCRIPTION.md`);
  }
}

// 删除旧缓存并重新加载以生成 .index.json
await invalidateIndexCache(skillsDir);

console.log('\nLoading skills with cache...');
const { skills: loadedSkills, categories } = await loadSkillsWithCache(skillsDir, 'user');

console.log(`\nLoaded ${loadedSkills.length} skills, ${categories.size} categories:`);
for (const [cat, desc] of categories) {
  console.log(`  ${cat}: ${desc}`);
}

// 验证 .index.json
const indexPath = path.join(skillsDir, '.index.json');
if (fs.existsSync(indexPath)) {
  const cache = JSON.parse(fs.readFileSync(indexPath, 'utf-8'));
  console.log(`\n.index.json created:`);
  console.log(`  version: ${cache.version}`);
  console.log(`  skills: ${cache.skills.length}`);
  console.log(`  categories: ${Object.keys(cache.categories).join(', ')}`);
  console.log(`  manifest entries: ${Object.keys(cache.manifest).length}`);
  for (const entry of cache.skills) {
    console.log(`    - ${entry.name} (category: ${entry.category || 'none'}, path: ${entry.filePath})`);
  }
}

// 列出生成的文件结构
function listFiles(dir: string, prefix = ''): void {
  for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
    if (entry.name.startsWith('.') && entry.isFile()) {
      // Show dot files (like .index.json) but with indicator
      console.log(`${prefix}${entry.name} (cache)`);
      continue;
    }
    if (entry.name.startsWith('.')) continue;
    if (entry.isDirectory()) {
      console.log(`${prefix}${entry.name}/`);
      listFiles(path.join(dir, entry.name), `${prefix}  `);
    } else {
      console.log(`${prefix}${entry.name}`);
    }
  }
}

console.log('\nGenerated file structure:');
listFiles(skillsDir);

console.log(`\nOutput directory: ${outputDir}`);
