#!/usr/bin/env bash
# workflow-config — read/write workflow config with dot-notation
# Usage:
#   workflow-config get <dotted.key> [default]
#   workflow-config set <dotted.key> <value>
#   workflow-config has <dotted.key>  → exit 0 if truthy, 1 if falsy/missing
set -euo pipefail

CONFIG_FILE="${WORKFLOW_CONFIG:-.claude/meowkit.config.json}"

# Resolve nested key: "features.costTracking" → d["features"]["costTracking"]
resolve_key() {
  python3 -c "
import json, sys
try:
    d = json.load(open('$CONFIG_FILE'))
    v = d
    for k in '$1'.split('.'):
        v = v[k]
    print(v if not isinstance(v, bool) else str(v).lower())
except (KeyError, TypeError, FileNotFoundError):
    if len(sys.argv) > 1:
        print(sys.argv[1])
    else:
        sys.exit(1)
" "${2:-}" 2>/dev/null
}

case "${1:-}" in
  get)
    KEY="${2:?Usage: workflow-config get <dotted.key> [default]}"
    DEFAULT="${3:-}"
    if [ -f "$CONFIG_FILE" ]; then
      resolve_key "$KEY" "$DEFAULT"
    elif [ -n "$DEFAULT" ]; then
      echo "$DEFAULT"
    fi
    ;;
  has)
    KEY="${2:?Usage: workflow-config has <dotted.key>}"
    if [ -f "$CONFIG_FILE" ]; then
      VAL=$(resolve_key "$KEY" "false" 2>/dev/null || echo "false")
      case "$VAL" in true|1|yes) exit 0 ;; *) exit 1 ;; esac
    else
      exit 1
    fi
    ;;
  set)
    KEY="${2:?Usage: workflow-config set <dotted.key> <value>}"
    VALUE="${3:?Usage: workflow-config set <dotted.key> <value>}"
    [ -f "$CONFIG_FILE" ] || { echo "Config not found: $CONFIG_FILE" >&2; exit 1; }
    python3 -c "
import json
with open('$CONFIG_FILE') as f: d = json.load(f)
keys = '$KEY'.split('.')
obj = d
for k in keys[:-1]:
    obj = obj.setdefault(k, {})
val = '$VALUE'
if val.lower() in ('true','false'): val = val.lower() == 'true'
elif val.isdigit(): val = int(val)
obj[keys[-1]] = val
with open('$CONFIG_FILE','w') as f: json.dump(d, f, indent=2)
" 2>/dev/null
    ;;
  *)
    cat << 'HELP'
workflow-config — read/write workflow config
Usage:
  workflow-config get <dotted.key> [default]
  workflow-config set <dotted.key> <value>
  workflow-config has <dotted.key>
Examples:
  workflow-config get features.costTracking
  workflow-config has features.memory && echo enabled
  workflow-config set mode.default strict
HELP
    exit 1
    ;;
esac
