#!/usr/bin/env bash
# Dopo server_bootstrap.sh: avvia lo stack produzione
set -euo pipefail

DEPLOY_ROOT="${DEPLOY_ROOT:-$HOME/matchlivetv}"
if [[ -d /opt/matchlivetv ]]; then
  DEPLOY_ROOT="/opt/matchlivetv"
fi

cd "$DEPLOY_ROOT/infra"

if [[ ! -f .env ]]; then
  echo "Manca infra/.env — copia da .env.production.example e compila i secret"
  exit 1
fi

docker compose -f docker-compose.prod.yml --env-file .env up -d --build

echo "Attendo healthcheck Rails..."
sleep 30
curl -sf "http://127.0.0.1:3000/up" && echo " OK" || echo " FAIL — vedi: docker compose -f docker-compose.prod.yml logs rails"

echo "Seed demo (opzionale, una tantum):"
echo "  docker compose -f docker-compose.prod.yml exec rails bundle exec rails db:seed"
