#!/bin/sh
# cn-weekly — create or show the current weekly reflection thread.
#
# Package command for cnos.core (migrated from built-in in
# v3.37.0 per #184 command pipeline symmetry).
#
# The week is computed as (month / 4 + 1), matching the pre-migration
# cn_gtd.run_weekly behavior rather than calendar ISO weeks. The
# calendar-ISO-week upgrade is out of scope for #184.
#
# Environment:
#   CN_HUB_PATH       exported by Cn_command.dispatch — hub root

set -eu

: "${CN_HUB_PATH:?cn-weekly: CN_HUB_PATH not set (expected via cn dispatch)}"

year="$(date +%Y)"
month="$(date +%m)"
# Strip any leading zero so arithmetic works in POSIX sh
month_num=$((10#${month}))
week=$(( month_num / 4 + 1 ))
wk=$(printf "%02d" "${week}")

dir="${CN_HUB_PATH}/threads/reflections/weekly"
file="${dir}/${year}-W${wk}.md"

mkdir -p "${dir}"

if [ -f "${file}" ]; then
  echo "Weekly exists: ${file}"
  cat "${file}"
  exit 0
fi

ts="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
cat > "${file}" <<EOF
---
date: ${ts}
type: weekly
---

# Weekly Reflection

## Summary

## Key Accomplishments

## Challenges

## Next Week Focus
EOF

echo "Created: ${file}"
