#!/usr/bin/env bash
set -euo pipefail

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

ENV_FILE="${1:-infra/docker/.env.staging}"

if [[ ! -f "$ENV_FILE" ]]; then
  echo "Missing $ENV_FILE — copy infra/docker/.env.staging.example first."
  exit 1
fi

if [[ ! -f apps/api/data/secrets/install.env ]]; then
  python3 apps/api/scripts/bootstrap_install.py
fi

docker compose \
  -f infra/docker/docker-compose.staging.yml \
  --env-file "$ENV_FILE" \
  up -d --build

echo "Staging deploy complete. Verify: curl -k https://\$(grep FRONTEND_URL $ENV_FILE | cut -d= -f2)/api/v1/health"
