#!/usr/bin/env bash
# sync-up.sh — Push live ~/.hermes config changes UP to timmy-config repo.
# The harness is the source. The repo is the record.
# Only commits when there are REAL changes (not empty syncs).

set -euo pipefail

REPO_DIR="$HOME/.timmy/timmy-config"
HERMES_HOME="$HOME/.hermes"

log() { echo "[sync-up] $*"; }

# === Copy live config into repo ===
cp "$HERMES_HOME/config.yaml" "$REPO_DIR/config.yaml"

for f in "$HERMES_HOME"/playbooks/*.yaml; do
  [ -f "$f" ] && cp "$f" "$REPO_DIR/playbooks/"
done

for f in "$HERMES_HOME"/skins/*; do
  [ -f "$f" ] && cp "$f" "$REPO_DIR/skins/"
done

[ -f "$HERMES_HOME/channel_directory.json" ] && cp "$HERMES_HOME/channel_directory.json" "$REPO_DIR/"

# === Only commit if there are real diffs ===
cd "$REPO_DIR"
git add -A

# Check if there are staged changes
if git diff --cached --quiet; then
  log "No changes to sync."
  exit 0
fi

# Build a meaningful commit message from what actually changed
CHANGED=$(git diff --cached --name-only | tr '\n' ', ' | sed 's/,$//')
git commit -m "config: update ${CHANGED}"
git push
log "Pushed: ${CHANGED}"
