#!/usr/bin/env bash
# ─────────────────────────────────────────────────────────────────────────────
# deploy.sh  —  Runs on the droplet during CI/CD deployment
# Called by GitHub Actions via SSH after a release is merged to main.
# ─────────────────────────────────────────────────────────────────────────────
set -euo pipefail

APP_DIR="/var/www/warehouse.soldered.com"

echo "==> Pulling latest code from main"
cd "$APP_DIR"
git fetch origin
git reset --hard origin/main

echo "==> Installing/updating Python dependencies"
.venv/bin/pip install -r requirements.txt -q

echo "==> Restarting service"
sudo systemctl restart warehouse

echo "==> Waiting for service to come up"
sleep 2
if systemctl is-active --quiet warehouse; then
    VERSION=$(cat "$APP_DIR/VERSION" 2>/dev/null || echo "unknown")
    echo "==> Warehouse App v${VERSION} is live"
else
    echo "!! Service failed to start"
    journalctl -u warehouse --no-pager -n 30
    exit 1
fi
