#!/bin/bash
# Install git post-commit hook for Zero-Touch Ingestion

# Assume run from repo root or clewso root
REPO_ROOT=$(git rev-parse --show-toplevel)
HOOK_DIR="$REPO_ROOT/.git/hooks"

echo "Installing hook to $HOOK_DIR..."

cat > "$HOOK_DIR/post-commit" <<EOF
#!/bin/bash
# Clewso Zero-Touch Ingestion Hook
# Triggers ingestion container in background after every commit.

echo "🧶 Clewso: Triggering background ingestion..."

# Navigate to repo root where docker-compose.yml lives
REPO_ROOT=\$(git rev-parse --show-toplevel)
cd "\$REPO_ROOT" || exit 1

# Run ingestion in background (-d) and remove container after (--rm)
# We ingest the mapped /app/repo (which is the repo root)
docker-compose run -d --rm ingestion python -m src.ingest /app/repo > /dev/null 2>&1 &

EOF

chmod +x "$HOOK_DIR/post-commit"
echo "✅ Clewso post-commit hook installed! (Zero-Touch Enabled)"
