#!/bin/bash

# Create a new skill scaffold

set -e

if [ $# -eq 0 ]; then
    echo "Usage: $0 <skill_name>"
    echo "Example: $0 bdd-testing"
    exit 1
fi

SKILL_NAME=$1
SKILL_DIR=".vibe/skills/${SKILL_NAME}"

# Convert underscores to hyphens for the skill name
SKILL_NAME_HYPHENATED=$(echo "$SKILL_NAME" | tr '_' '-')

# Create skill directory
mkdir -p "$SKILL_DIR"

# Validate path - ensure we're not creating nested .vibe directories
if [[ "$SKILL_DIR" == *".vibe/.vibe"* ]]; then
    echo "❌ Error: Detected nested .vibe path: $SKILL_DIR"
    echo "This usually happens when running the script from within .vibe/skills/"
    echo "Please run from project root or use absolute paths"
    exit 1
fi

# Show the actual path being created
echo "✓ Creating skill in: $(pwd)/$SKILL_DIR"

# Create SKILL.md with basic template
cat > "$SKILL_DIR/SKILL.md" <<EOL
---
name: ${SKILL_NAME_HYPHENATED}
description: [Brief description of what this skill does and when to use it]
license: MIT
metadata:
  author: [Your Name or Organization]
  version: "1.0.0"
---

# ${SKILL_NAME_HYPHENATED}

[Detailed description of the skill's purpose and functionality]

## Commands

### [Command Name]

\`\`\`bash
[command usage]
\`\`\`

[Command description]

**Arguments:**
- `arg1` - Description
- `arg2` - Description

## Workflows

### [Workflow Name]

1. **Step 1**: [Description]
2. **Step 2**: [Description]
3. **Step 3**: [Description]

## Usage Examples

### [Example Name]

\`\`\`bash
[example code]
\`\`\`

## Best Practices

1. [Best practice 1]
2. [Best practice 2]
3. [Best practice 3]

## References

- [Reference 1](references/[reference-file].md)
- [Reference 2](references/[reference-file].md)
EOL

# Create optional directories
mkdir -p "$SKILL_DIR/scripts"
mkdir -p "$SKILL_DIR/references"
mkdir -p "$SKILL_DIR/assets"

# Create a basic example script
cat > "$SKILL_DIR/scripts/example.sh" <<EOL
#!/bin/bash

# Example script for ${SKILL_NAME_HYPHENATED} skill

set -e

echo "This is an example script for the ${SKILL_NAME_HYPHENATED} skill"
echo "Replace this with your actual script logic"

# Your script implementation goes here
# Example:
# echo "Processing..."
# [command] [arguments]
EOL

chmod +x "$SKILL_DIR/scripts/example.sh"

# Create a basic reference file
cat > "$SKILL_DIR/references/REFERENCE.md" <<EOL
# ${SKILL_NAME_HYPHENATED} Reference

## Overview

Detailed technical reference for the ${SKILL_NAME_HYPHENATED} skill.

## Key Concepts

### [Concept 1]

[Detailed explanation]

### [Concept 2]

[Detailed explanation]

## API Reference

### [Function/Method Name]

**Description**: [What it does]

**Parameters**:
- `param1` - [Type]: [Description]
- `param2` - [Type]: [Description]

**Returns**: [Return type and description]

**Example**:
\`\`\`bash
[example usage]
\`\`\`

## Troubleshooting

### [Issue 1]

**Symptoms**: [What the user sees]

**Cause**: [Root cause]

**Solution**: [How to fix it]

### [Issue 2]

**Symptoms**: [What the user sees]

**Cause**: [Root cause]

**Solution**: [How to fix it]
EOL

echo "✓ Created new skill: ${SKILL_NAME_HYPHENATED}"
echo "✓ Skill directory: ${SKILL_DIR}"
echo "✓ Created SKILL.md with template"
echo "✓ Created optional directories: scripts/, references/, assets/"
echo "✓ Created example script: ${SKILL_DIR}/scripts/example.sh"
echo "✓ Created reference file: ${SKILL_DIR}/references/REFERENCE.md"
echo ""
echo "Next steps:"
echo "1. Edit ${SKILL_DIR}/SKILL.md to add your skill details"
echo "2. Add your scripts to the scripts/ directory"
echo "3. Add documentation to the references/ directory"
echo "4. Add any assets to the assets/ directory"
echo "5. Validate your skill: .vibe/skills/skill_creator/scripts/validate_skill.sh ${SKILL_DIR}"