#!/usr/bin/env bash
# story: e13s03 e39s04
# Render helpers for sync-skills.sh — build_body + per-target render_* functions.
# Source after skill-common.sh; expects CURSOR_RULES, GEMINI_*, PI_*, OKF_WIKI_* globals.

if [ -n "${SYNC_RENDER_LOADED:-}" ]; then
  return 0
fi
SYNC_RENDER_LOADED=1

build_body() {
  local skill_md="$1"
  local skill_dir="$2"
  local body extra_md

  body=$(awk '/^---/{f++; next} f>=2{print}' "$skill_md")
  for extra_md in $(find "$skill_dir" -maxdepth 1 -name "*.md" ! -name "SKILL.md" | LC_ALL=C sort); do
    body="$body"$'\n\n'"---"$'\n\n'"$(cat "$extra_md")"
  done
  echo "$body" | grep -v 'disable-model-invocation'
}

render_cursor() {
  local cursor_file="$CURSOR_RULES/$IR_NAME.mdc"
  {
    echo "---"
    echo "description: \"$IR_DESC_ESCAPED\""
    echo "alwaysApply: false"
    echo "---"
    echo ""
    echo "$IR_BODY"
  } > "$cursor_file"
}

render_gemini_skill() {
  mkdir -p "$GEMINI_SKILLS/$IR_NAME"
  {
    echo "---"
    echo "name: $IR_NAME"
    echo "description: \"$IR_DESC_ESCAPED\""
    echo "---"
    echo ""
    echo "$IR_BODY"
  } > "$GEMINI_SKILLS/$IR_NAME/SKILL.md"
}

render_gemini_command() {
  mkdir -p "$GEMINI_COMMANDS/prompts"
  local prompt_file="commands/prompts/$IR_NAME.md"
  echo "$IR_BODY" > "$GEMINI_EXT_DIR/$prompt_file"
  {
    echo "description = \"$IR_DESC_ESCAPED\""
    echo "prompt = \"@{$prompt_file}\""
  } > "$GEMINI_COMMANDS/$IR_NAME.toml"
}

render_pi_skill() {
  mkdir -p "$PI_SKILLS/$IR_NAME"
  {
    echo "---"
    echo "name: $IR_NAME"
    echo "description: \"$IR_DESC_ESCAPED\""
    [[ -n "$IR_MODEL" ]] && echo "model: $IR_MODEL"
    echo "---"
    echo ""
    echo "$IR_BODY"
  } > "$PI_SKILLS/$IR_NAME/SKILL.md"
}

render_pi_prompt() {
  {
    echo "---"
    echo "description: $IR_DESCRIPTION"
    echo "---"
    echo ""
    echo "$IR_BODY"
  } > "$PI_PROMPTS/$IR_NAME.md"
}

render_okf_concept() {
  mkdir -p "$OKF_WIKI_SKILLS"
  local okf_file="$OKF_WIKI_SKILLS/$IR_NAME.md"
  local phase="utility" title desc_escaped short_desc effort_val="${_PF_EFFORT:-standard}"

  case "$IR_NAME" in
    survey-context|elaborate-spec|model-domain|define-language|\
    design-interface|plan-work|plan-release|slice-tasks|\
    kickoff-branch|develop-tdd|verify-work|audit-code|\
    commit-message|release-branch|security-review|\
    assess-impact|investigate-bug|validate-fix|dispatch-agents|\
    request-review|respond-review|hook-commits|guard-git|\
    wire-observability|wire-ci|smoke-test|deploy|enforce-first)
      phase="critical-path" ;;
  esac

  title=$(echo "$IR_NAME" | sed -E 's/-/ /g; s/\b(.)/\U\1/g')
  desc_escaped=$(echo "$IR_DESCRIPTION" | sed 's/\\/\\\\/g; s/"/\\"/g')
  short_desc="$IR_DESCRIPTION"
  [[ ${#short_desc} -gt 200 ]] && short_desc="${short_desc:0:197}..."

  {
    echo "---"
    echo "okf_kind: concept"
    echo "type: Skill"
    echo "id: $IR_NAME"
    echo "title: \"$title\""
    echo "name: $IR_NAME"
    echo "category: skills"
    [[ -n "$IR_MODEL" ]] && echo "model: $IR_MODEL"
    echo "effort: $effort_val"
    echo "phase: $phase"
    echo "description: \"$desc_escaped\""
    echo "references: []"
    echo "---"
    echo ""
    echo "# $title"
    echo ""
    echo "**Phase:** $phase"
    [[ -n "$IR_MODEL" ]] && echo "**Model:** $IR_MODEL"
    echo "**Effort:** $effort_val"
    echo ""
    echo "$IR_DESCRIPTION"
    echo ""
    echo "> Auto-generated by sync-skills.sh --okf (e39s04) from $SKILLS_ROOT/$IR_NAME/SKILL.md"
  } > "$okf_file"
}
