#!/bin/sh
set -eu

SLOT="${1:-}"
FLAG="${2:-}"

if [ -z "$SLOT" ] || [ "$SLOT" = "--help" ] || [ "$SLOT" = "-h" ]; then
  echo "Usage:"
  echo "  $0 green [--no-cache]        # build+up+health+smoke sur green"
  echo "  $0 blue  [--no-cache]        # build+up+health+smoke sur blue"
  echo ""
  echo "Options:"
  echo "  --no-cache       build sans cache (recommandé si vars .env changent)"
  echo "  --clean          DRY-RUN nettoyage artefacts macOS"
  echo "  --clean-apply    nettoyage APPLY (supprime réellement)"
  exit 0
fi

case "$SLOT" in
  blue)  SERVICE="web_blue";  PORT="8081"; CNAME="archicratie-web-blue" ;;
  green) SERVICE="web_green"; PORT="8082"; CNAME="archicratie-web-green" ;;
  *) echo "Slot invalide: $SLOT (attendu: blue|green)"; exit 1 ;;
esac

ROOT="$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)"
cd "$ROOT"

# Flags
NO_CACHE=0
DO_CLEAN=0
CLEAN_APPLY=0

# parse second arg only (simple & cockpit)
case "${FLAG:-}" in
  --no-cache) NO_CACHE=1 ;;
  --clean) DO_CLEAN=1 ;;
  --clean-apply) DO_CLEAN=1; CLEAN_APPLY=1 ;;
  "") ;;
  *) echo "Flag invalide: $FLAG"; exit 1 ;;
esac

echo "== deploy-slot =="
echo "slot     : $SLOT"
echo "service  : $SERVICE"
echo "port     : $PORT"
echo "no-cache : $NO_CACHE"
echo "clean    : $DO_CLEAN (apply=$CLEAN_APPLY)"
echo ""

if [ ! -f "docker-compose.yml" ]; then
  echo "ERREUR: docker-compose.yml introuvable dans $ROOT"
  exit 1
fi

if [ "$DO_CLEAN" -eq 1 ]; then
  if [ "$CLEAN_APPLY" -eq 1 ]; then
    ./scripts/clean-macos-artifacts.sh --apply
  else
    ./scripts/clean-macos-artifacts.sh
  fi
  echo ""
fi

# BuildKit (meilleur sur DSM)
export DOCKER_BUILDKIT=1
export COMPOSE_DOCKER_CLI_BUILD=1

echo "== 1) Build $SERVICE =="
if [ "$NO_CACHE" -eq 1 ]; then
  docker compose build --no-cache "$SERVICE"
else
  docker compose build "$SERVICE"
fi

echo ""
echo "== 2) Up (recreate) $SERVICE =="
docker compose up -d --force-recreate "$SERVICE"

echo ""
echo "== 3) Wait health ($CNAME) =="
# attente max ~120s
i=0
while :; do
  STATUS="$(docker inspect -f '{{.State.Health.Status}}' "$CNAME" 2>/dev/null || echo "unknown")"
  echo "health: $STATUS"
  if [ "$STATUS" = "healthy" ]; then
    break
  fi
  i=$((i+1))
  if [ "$i" -ge 24 ]; then
    echo "ERREUR: health n'est pas devenu healthy (timeout)."
    echo "Logs:"
    docker compose logs --tail=120 "$SERVICE" || true
    exit 2
  fi
  sleep 5
done

echo ""
echo "== 4) Smoke test (port $PORT) =="
if [ -x "./scripts/smoke.sh" ]; then
  ./scripts/smoke.sh "$PORT"
else
  echo "WARN: scripts/smoke.sh absent ou non exécutable."
  echo "Test minimal:"
  curl -I "http://127.0.0.1:$PORT/" | head -n 8 || true
  curl -I "http://127.0.0.1:$PORT/pagefind/pagefind.js" | head -n 8 || true
fi

echo ""
echo "== OK =="
echo "Slot $SLOT est prêt."
echo ""
echo "ACTION DSM (bascule prod / rollback 10s) :"
echo "DSM → Portail des applications → Proxy inversé → règle archicratie.trans-hands.synology.me"
echo "Destination: http://127.0.0.1:$PORT"
echo ""
echo "Vérif immédiate:"
echo "curl -kI https://archicratie.trans-hands.synology.me/ | head"
