#!/usr/bin/env bash
# orch-goal-status — show the current active goal, linked tasks, and budget.
#
# Reads SESH_GOAL_ID from env (set by orch-goal-pursue). Renders the goal
# record and a one-line summary of each linked task.
#
# Usage:
#   orch-goal-status [<goal-id>]
#
# If <goal-id> is omitted, uses $SESH_GOAL_ID.

set -u

GOAL_ID="${1:-${SESH_GOAL_ID:-}}"

if [[ -z "$GOAL_ID" ]]; then
  echo "no active goal in this session" >&2
  echo "set SESH_GOAL_ID or pass <goal-id>; use \`orch-goal-pursue\` to start one" >&2
  exit 2
fi

SESH_OPS_BIN="${SESH_OPS_BIN:-sesh-ops}"
SCOPE="${SESH_GOAL_SCOPE:-project}"
SCOPE_ID="${SESH_GOAL_SCOPE_ID:-$(basename "$PWD" | tr .- _)}"

if ! command -v "$SESH_OPS_BIN" >/dev/null 2>&1; then
  echo "error: $SESH_OPS_BIN not on PATH" >&2; exit 3
fi

# Render the goal record (human-readable from sesh-ops).
"$SESH_OPS_BIN" --scope "$SCOPE" --scope-id "$SCOPE_ID" goal status "$GOAL_ID"

# Then list linked tasks with their status.
TASKS_JSON="$("$SESH_OPS_BIN" --scope "$SCOPE" --scope-id "$SCOPE_ID" goal get "$GOAL_ID" 2>/dev/null | jq -r '.tasks // [] | .[]')"
if [[ -n "$TASKS_JSON" ]]; then
  echo
  echo "linked tasks:"
  while IFS= read -r tid; do
    [[ -z "$tid" ]] && continue
    "$SESH_OPS_BIN" --scope "$SCOPE" --scope-id "$SCOPE_ID" task get "$tid" 2>/dev/null \
      | jq -r '"  \(.id)  [\(.status)]  \(.title)"'
  done <<<"$TASKS_JSON"
fi

# Show the cross-harness token-accounting daemon status.
DAEMON_PID_FILE="$HOME/.cache/orch-goal-daemon/${GOAL_ID}.pid"
echo
echo "token-accounting daemon:"
if [[ -f "$DAEMON_PID_FILE" ]]; then
  DPID="$(cat "$DAEMON_PID_FILE")"
  if kill -0 "$DPID" 2>/dev/null; then
    echo "  running (pid=$DPID, pidfile=$DAEMON_PID_FILE)"
  else
    echo "  stopped (stale pidfile at $DAEMON_PID_FILE — pid=$DPID no longer alive)"
  fi
else
  echo "  not running (no pidfile at $DAEMON_PID_FILE)"
  echo "  launch with: orch-goal-stop-account-daemon (or re-run orch-goal-pursue)"
fi
