#!/usr/bin/env bash
# CapInt task helper — route + optional pipeline preflight
# Usage: ./scripts/capint-task.sh "prd hazırla"
#        CAPINT_DEFAULT_CONFIRM=plan_first ./scripts/capint-task.sh "i18n çeviri"

set -euo pipefail

TASK="${*:-}"
if [ -z "$TASK" ]; then
  echo 'Usage: capint-task.sh "your task text"' >&2
  exit 1
fi

if command -v capint >/dev/null 2>&1; then
  CAPINT=(capint)
elif [ -f "$(dirname "$0")/../bin/capint.js" ]; then
  CAPINT=(node "$(dirname "$0")/../bin/capint.js")
else
  echo "capint not found — npm install -g @bcelep/capint" >&2
  exit 1
fi

"${CAPINT[@]}" pipeline "$TASK" --force 2>/dev/null || "${CAPINT[@]}" doctor
"${CAPINT[@]}" route --json "$TASK" | node -e "
const chunks = [];
process.stdin.on('data', d => chunks.push(d));
process.stdin.on('end', () => {
  const data = JSON.parse(Buffer.concat(chunks).toString());
  const ei = data.execution_intent;
  console.log(JSON.stringify({
    intent: ei.intent,
    capability: ei.capability,
    resolution: ei.resolution,
    resolution_status: ei.resolution_status,
    plan: ei.plan,
    confirm_default: ei.confirm_default_option,
    files_to_read: ei.explanation?.files_to_read || []
  }, null, 2));
});
"
