/*
 * Copyright 2026, Jaime Terrats.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
 * Markdown content for the Elements Sync skill.
 * Creates Requirements and Stories in Elements.cloud from story maps
 * and other structured sources using the Elements REST API.
 */
export function elementsSyncSkillMd() {
    const lines = [
        '---',
        'name: elements-sync',
        'description: >-',
        '  Create and update Requirements and Stories in Elements.cloud from story maps,',
        '  backlog documents, or free-form descriptions. Uses the Elements REST API',
        '  (https://api.elements.cloud/v1) with ELEMENTS_API_KEY and ELEMENTS_SPACE_NAME',
        '  environment variables. USE FOR: push to elements, create requirement, create story,',
        '  sync elements, elements cloud, upload stories, elements backlog, push requirements.',
        '  Reusable by BA, PM, and Architect profiles.',
        '---',
        '',
        '# Elements Sync',
        '',
        '## Prerequisites',
        '',
        '| Requirement | Check | How to obtain |',
        '|-------------|-------|---------------|',
        '| `ELEMENTS_API_KEY` env var | `echo $ELEMENTS_API_KEY` | Elements Space → Developer → API Tokens → Create |',
        '| `ELEMENTS_SPACE_NAME` env var | `echo $ELEMENTS_SPACE_NAME` | Your Elements Space name (shown in the app URL) |',
        '| `curl` | `curl --version` | Pre-installed on macOS/Linux |',
        '',
        '> **Never hardcode credentials.** Always read from environment variables.',
        '> If variables are not set, instruct the user to add them to their shell profile',
        '> or `.env` file and **never commit them to version control**.',
        '',
        '---',
        '',
        '## Active Job Monitoring (CRITICAL)',
        '',
        'When creating multiple items, create them **one by one** and report each result.',
        'Never batch-create without tracking. After all items are created, report:',
        'total created, any failures with error details, and the IDs of created items.',
        '',
        '---',
        '',
        '## Environment Setup',
        '',
        '```bash',
        '# Add to ~/.zshrc or ~/.bash_profile',
        'export ELEMENTS_API_KEY="your-api-key-here"',
        'export ELEMENTS_SPACE_NAME="your-space-name"',
        '```',
        '',
        '> Obtain your API key: Elements app → Space Management → Developer → API Tokens → Create API Token.',
        '> The key is shown **only once** — store it in a password manager.',
        '',
        '---',
        '',
        '## Using the Helper Script',
        '',
        '```bash',
        '# Create a Requirement',
        'bash .setup-agents/skills/elements-sync/scripts/push-to-elements.sh requirement \\',
        '  --summary "Implement vacation days feature" \\',
        '  --what "Users need a way to track remaining vacation days" \\',
        '  --priority 1 --impact 2 --risk 1',
        '',
        '# Create a Story',
        'bash .setup-agents/skills/elements-sync/scripts/push-to-elements.sh story \\',
        '  --summary "As a user I want to see my vacation balance" \\',
        '  --description "Display remaining days on the home dashboard" \\',
        '  --risk 1',
        '',
        '# Create a Story linked to a Requirement',
        'bash .setup-agents/skills/elements-sync/scripts/push-to-elements.sh story \\',
        '  --summary "Show vacation balance on dashboard" \\',
        '  --requirement-id "642d4765518823ea234f104e" \\',
        '  --risk 1',
        '```',
        '',
        '---',
        '',
        '## API Reference',
        '',
        '### Base URL',
        '',
        '```',
        'https://api.elements.cloud/v1',
        '```',
        '',
        '### Authentication',
        '',
        'Every request must include the header:',
        '```',
        'X-API-KEY: $ELEMENTS_API_KEY',
        '```',
        '',
        '### Create a Requirement — `POST /requirements`',
        '',
        'Required fields: `summary`, `priority`, `impact`, `risk`',
        '',
        '```json',
        '{',
        '  "summary": "string (required)",',
        '  "whatIsRequired": "string — what must be done",',
        '  "howItMightBeImplemented": "string — implementation notes",',
        '  "requiredByReason": "string — business justification",',
        '  "requiredBy": "YYYY-MM-DD — target date",',
        '  "priority": 1,',
        '  "impact": 2,',
        '  "risk": 1,',
        '  "tags": ["tag1", "tag2"],',
        '  "release": "Release Name",',
        '  "assignee": "user@example.com"',
        '}',
        '```',
        '',
        '`priority`, `impact`, `risk` are integers (typically 1–3, where 1 = high).',
        '',
        '### Create a Story — `POST /stories`',
        '',
        'Required field: `summary`',
        '',
        '```json',
        '{',
        '  "summary": "string (required)",',
        '  "description": "string — As a user I want...",',
        '  "acceptanceCriteria": "string — Given/When/Then",',
        '  "requirement": "requirementId — links story to parent requirement",',
        '  "affectedRoles": ["Role Name"],',
        '  "risk": 1,',
        '  "tags": ["tag1"],',
        '  "release": "Release Name",',
        '  "assignee": "user@example.com",',
        '  "externalId": "US-101 — your tracking ID"',
        '}',
        '```',
        '',
        '### Responses',
        '',
        '| Code | Meaning |',
        '|------|---------|',
        '| 201 | Created — response body contains the full object with `id` |',
        '| 400 | Validation error — check required fields |',
        '| 401 | Unauthorized — verify `ELEMENTS_API_KEY` |',
        '| 429 | Rate limited — max 100 req/s, 5 000 req/month per Space |',
        '',
        '---',
        '',
        '## Field Mapping from Story Maps',
        '',
        'When reading a story map document to push to Elements, use this mapping:',
        '',
        '| Story Map field | Elements Requirement | Elements Story |',
        '|-----------------|---------------------|----------------|',
        '| Epic title | `summary` | — |',
        '| Epic description | `whatIsRequired` | — |',
        '| User story text | — | `summary` |',
        '| Acceptance criteria | — | `acceptanceCriteria` |',
        '| Priority (P1/P2/P3) | `priority` (1/2/3) | `risk` (1/2/3) |',
        '| US ID (e.g. US-101) | — | `externalId` |',
        '| Epic → Story link | — | `requirement` (parent ID) |',
        '',
        '---',
        '',
        '## Agent Workflow',
        '',
        '1. **Verify credentials** — check `ELEMENTS_API_KEY` and `ELEMENTS_SPACE_NAME` are set.',
        '   If missing, show the Environment Setup section above.',
        '2. **Parse the source** — read the story map or backlog document.',
        '3. **Create Requirements first** — one per Epic. Record the returned `id`.',
        '4. **Create Stories** — one per user story, setting `requirement` to the parent Requirement ID.',
        '5. **Report results** — list each created item with its Elements ID and summary.',
        '   Flag any failures with the error message.',
        '',
        '### Error Handling',
        '',
        '- `400 validation_failed`: read the `error` field and fix the missing/invalid field.',
        '- `401 Unauthorized`: remind the user to verify `ELEMENTS_API_KEY`.',
        '- `429 Rate Limited`: wait and retry; the `X-RateLimit-Reset` header says when.',
        '- Network error: verify the machine has internet access to `api.elements.cloud`.',
    ];
    return lines.join('\n');
}
export function pushToElementsScript() {
    const lines = [
        '#!/usr/bin/env bash',
        '# Pushes a Requirement or Story to Elements.cloud via the REST API.',
        '#',
        '# Usage:',
        '#   push-to-elements.sh requirement --summary "..." --what "..." --priority 1 --impact 2 --risk 1',
        '#   push-to-elements.sh story       --summary "..." [--description "..."] [--risk 1] [--requirement-id "id"]',
        '#',
        '# Required env vars: ELEMENTS_API_KEY',
        'set -euo pipefail',
        '',
        '# ── Dependency guard ─────────────────────────────────────────────────────',
        'if ! command -v curl &>/dev/null; then',
        '  echo "ERROR: curl not found. Install curl via your package manager."',
        '  exit 1',
        'fi',
        '',
        'if [ -z "${ELEMENTS_API_KEY:-}" ]; then',
        '  echo "ERROR: ELEMENTS_API_KEY is not set."',
        '  echo "Obtain your key: Elements app → Space Management → Developer → API Tokens → Create API Token"',
        '  echo "Then run: export ELEMENTS_API_KEY=\\"your-key\\""',
        '  exit 1',
        'fi',
        '',
        'RESOURCE="${1:?Usage: push-to-elements.sh <requirement|story> [options]}"',
        'shift',
        '',
        'if [[ "$RESOURCE" != "requirement" && "$RESOURCE" != "story" ]]; then',
        '  echo "ERROR: First argument must be \\"requirement\\" or \\"story\\", got: $RESOURCE"',
        '  exit 1',
        'fi',
        '',
        '# ── Parse arguments ──────────────────────────────────────────────────────',
        'SUMMARY=""',
        'DESCRIPTION=""',
        'WHAT_IS_REQUIRED=""',
        'HOW_IMPLEMENTED=""',
        'REQUIRED_BY_REASON=""',
        'REQUIRED_BY=""',
        'PRIORITY=""',
        'IMPACT=""',
        'RISK=""',
        'TAGS=""',
        'RELEASE=""',
        'ASSIGNEE=""',
        'REQUIREMENT_ID=""',
        'EXTERNAL_ID=""',
        'ACCEPTANCE_CRITERIA=""',
        '',
        'while [[ $# -gt 0 ]]; do',
        '  case "$1" in',
        '    --summary)           SUMMARY="$2";           shift 2 ;;',
        '    --description)       DESCRIPTION="$2";       shift 2 ;;',
        '    --what)              WHAT_IS_REQUIRED="$2";  shift 2 ;;',
        '    --how)               HOW_IMPLEMENTED="$2";   shift 2 ;;',
        '    --reason)            REQUIRED_BY_REASON="$2";shift 2 ;;',
        '    --required-by)       REQUIRED_BY="$2";       shift 2 ;;',
        '    --priority)          PRIORITY="$2";          shift 2 ;;',
        '    --impact)            IMPACT="$2";            shift 2 ;;',
        '    --risk)              RISK="$2";              shift 2 ;;',
        '    --tags)              TAGS="$2";              shift 2 ;;',
        '    --release)           RELEASE="$2";           shift 2 ;;',
        '    --assignee)          ASSIGNEE="$2";          shift 2 ;;',
        '    --requirement-id)    REQUIREMENT_ID="$2";    shift 2 ;;',
        '    --external-id)       EXTERNAL_ID="$2";       shift 2 ;;',
        '    --acceptance)        ACCEPTANCE_CRITERIA="$2"; shift 2 ;;',
        '    *) echo "Unknown option: $1"; exit 1 ;;',
        '  esac',
        'done',
        '',
        'if [ -z "$SUMMARY" ]; then',
        '  echo "ERROR: --summary is required"',
        '  exit 1',
        'fi',
        '',
        '# ── Build JSON payload ───────────────────────────────────────────────────',
        'BASE_URL="https://api.elements.cloud/v1"',
        '',
        'build_requirement_json() {',
        '  local json',
        '  json=$(printf \'{"summary":"%s"\' "$(echo "$SUMMARY" | sed \'s/"/\\\\"/g\')")',
        '  [ -n "$WHAT_IS_REQUIRED" ]   && json="$json,\\"whatIsRequired\\":\\"$(echo "$WHAT_IS_REQUIRED" | sed \'s/"/\\\\"/g\')\\""',
        '  [ -n "$HOW_IMPLEMENTED" ]    && json="$json,\\"howItMightBeImplemented\\":\\"$(echo "$HOW_IMPLEMENTED" | sed \'s/"/\\\\"/g\')\\""',
        '  [ -n "$REQUIRED_BY_REASON" ] && json="$json,\\"requiredByReason\\":\\"$(echo "$REQUIRED_BY_REASON" | sed \'s/"/\\\\"/g\')\\""',
        '  [ -n "$REQUIRED_BY" ]        && json="$json,\\"requiredBy\\":\\"$REQUIRED_BY\\""',
        '  [ -n "$PRIORITY" ]           && json="$json,\\"priority\\":$PRIORITY"',
        '  [ -n "$IMPACT" ]             && json="$json,\\"impact\\":$IMPACT"',
        '  [ -n "$RISK" ]               && json="$json,\\"risk\\":$RISK"',
        '  [ -n "$RELEASE" ]            && json="$json,\\"release\\":\\"$(echo "$RELEASE" | sed \'s/"/\\\\"/g\')\\""',
        '  [ -n "$ASSIGNEE" ]           && json="$json,\\"assignee\\":\\"$ASSIGNEE\\""',
        '  echo "$json}"',
        '}',
        '',
        'build_story_json() {',
        '  local json',
        '  json=$(printf \'{"summary":"%s"\' "$(echo "$SUMMARY" | sed \'s/"/\\\\"/g\')")',
        '  [ -n "$DESCRIPTION" ]        && json="$json,\\"description\\":\\"$(echo "$DESCRIPTION" | sed \'s/"/\\\\"/g\')\\""',
        '  [ -n "$ACCEPTANCE_CRITERIA" ]&& json="$json,\\"acceptanceCriteria\\":\\"$(echo "$ACCEPTANCE_CRITERIA" | sed \'s/"/\\\\"/g\')\\""',
        '  [ -n "$REQUIREMENT_ID" ]     && json="$json,\\"requirement\\":\\"$REQUIREMENT_ID\\""',
        '  [ -n "$EXTERNAL_ID" ]        && json="$json,\\"externalId\\":\\"$EXTERNAL_ID\\""',
        '  [ -n "$RISK" ]               && json="$json,\\"risk\\":$RISK"',
        '  [ -n "$RELEASE" ]            && json="$json,\\"release\\":\\"$(echo "$RELEASE" | sed \'s/"/\\\\"/g\')\\""',
        '  [ -n "$ASSIGNEE" ]           && json="$json,\\"assignee\\":\\"$ASSIGNEE\\""',
        '  echo "$json}"',
        '}',
        '',
        '# ── Call API ─────────────────────────────────────────────────────────────',
        'if [ "$RESOURCE" = "requirement" ]; then',
        '  if [ -z "$PRIORITY" ] || [ -z "$IMPACT" ] || [ -z "$RISK" ]; then',
        '    echo "ERROR: Requirements need --priority, --impact, and --risk"',
        '    exit 1',
        '  fi',
        '  PAYLOAD=$(build_requirement_json)',
        '  ENDPOINT="$BASE_URL/requirements"',
        'else',
        '  PAYLOAD=$(build_story_json)',
        '  ENDPOINT="$BASE_URL/stories"',
        'fi',
        '',
        'echo "Creating $RESOURCE in Elements.cloud..."',
        '',
        'RESPONSE=$(curl -s -w "\\n%{http_code}" -X POST "$ENDPOINT" \\',
        '  -H "X-API-KEY: ${ELEMENTS_API_KEY}" \\',
        '  -H "Content-Type: application/json" \\',
        '  -d "$PAYLOAD")',
        '',
        'HTTP_CODE=$(echo "$RESPONSE" | tail -1)',
        'BODY=$(echo "$RESPONSE" | sed \'$d\')',
        '',
        'if [ "$HTTP_CODE" = "201" ]; then',
        '  ITEM_ID=$(echo "$BODY" | grep -o \'"id":"[^"]*"\' | head -1 | cut -d\'"\' -f4 || true)',
        '  echo "✓ Created $RESOURCE: $ITEM_ID"',
        '  echo "  Summary: $SUMMARY"',
        '  echo "$BODY"',
        'elif [ "$HTTP_CODE" = "401" ]; then',
        '  echo "ERROR: Authentication failed (401). Check your ELEMENTS_API_KEY."',
        '  exit 1',
        'elif [ "$HTTP_CODE" = "429" ]; then',
        '  echo "ERROR: Rate limited (429). Wait before retrying."',
        '  echo "$BODY"',
        '  exit 1',
        'else',
        '  echo "ERROR: API returned HTTP $HTTP_CODE"',
        '  echo "$BODY"',
        '  exit 1',
        'fi',
    ];
    return lines.join('\n');
}
//# sourceMappingURL=elements-sync-content.js.map