# Standalone single-box deploy: Gluecron + Postgres(pgvector) + Caddy(auto-HTTPS)
# plus self-healing (autoheal), log rotation, daily backups, and fast auto-deploy.
#
# For a DEDICATED VPS where Gluecron owns ports 80/443 (no co-tenant). One
# command brings up the whole stack with automatic Let's Encrypt certs:
#
#   docker compose -f docker-compose.standalone.yml up -d --build
#
# Variables come from a local .env (POSTGRES_PASSWORD, optional ANTHROPIC_API_KEY)
# — see scripts/standalone-deploy.sh which generates it and installs the backup
# + auto-deploy systemd timers. The Caddyfile already serves gluecron.com + www.

x-logging: &default-logging
  driver: json-file
  options:
    max-size: "10m"
    max-file: "3"

services:
  postgres:
    # pgvector image is required: migration 0057 does CREATE EXTENSION vector.
    image: pgvector/pgvector:pg16
    environment:
      POSTGRES_USER: gluecron
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: gluecron
    volumes:
      - pgdata:/var/lib/postgresql/data
    restart: unless-stopped
    logging: *default-logging
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U gluecron -d gluecron"]
      interval: 10s
      timeout: 5s
      retries: 6

  gluecron:
    build: .
    # Pass the ENTIRE .env into the container. Previously only a hardcoded
    # allowlist below reached the app, so any env-gated feature whose var
    # wasn't hand-added here was silently dead even when set in .env —
    # SERVER_TARGETS_KEY (deploy targets), WORKFLOW_SECRETS_KEY (workflow
    # secrets), the VAPRON_* deploy vars, etc. env_file fixes the whole
    # class: set a documented var in .env and it just works. The explicit
    # `environment:` block below still wins for the literals it pins
    # (PORT, NODE_ENV, GIT_REPOS_PATH, APP_BASE_URL, SSH_PORT) and stays as
    # documentation of the load-bearing ones.
    env_file:
      - .env
    environment:
      - DATABASE_URL=${DATABASE_URL:-}
      - GIT_REPOS_PATH=/data/repos
      - PORT=3000
      - NODE_ENV=production
      - APP_BASE_URL=https://gluecron.com
      - SSH_PORT=0
      - ANTHROPIC_API_KEY=${ANTHROPIC_API_KEY:-}
      # Build provenance. The image has no .git, so without these
      # src/lib/build-info.ts falls through to "unknown" — which froze the
      # footer at "unknown · unknown", made /api/version report a SHA that
      # never changes (so the auto-update banner could never fire), and left
      # the PWA service-worker cache key static across deploys.
      # GIT_SHA is what build-info.ts reads; BUILD_SHA is what version.ts and
      # pwa.ts read. Both are exported by scripts/self-deploy.sh.
      - GIT_SHA=${GIT_SHA:-}
      - GIT_BRANCH=${GIT_BRANCH:-main}
      - BUILD_SHA=${BUILD_SHA:-}
      # "Sign in with Google" — values come from .env (gitignored). Wiring them
      # here is what makes the env bootstrap reach the container and survive
      # redeploys; a config saved at /admin/google-oauth (DB) still takes
      # precedence over these.
      - GOOGLE_OAUTH_CLIENT_ID=${GOOGLE_OAUTH_CLIENT_ID:-}
      - GOOGLE_OAUTH_CLIENT_SECRET=${GOOGLE_OAUTH_CLIENT_SECRET:-}
      - GOOGLE_OAUTH_AUTO_CREATE=${GOOGLE_OAUTH_AUTO_CREATE:-}
      - GOOGLE_OAUTH_ALLOWED_DOMAINS=${GOOGLE_OAUTH_ALLOWED_DOMAINS:-}
    expose:
      - "3000"
    volumes:
      - git-repos:/data/repos
    depends_on:
      postgres:
        condition: service_healthy
    restart: unless-stopped
    logging: *default-logging
    healthcheck:
      # Liveness — autoheal restarts the container if this fails repeatedly.
      test: ["CMD", "wget", "-qO-", "http://localhost:3000/healthz"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s

  caddy:
    image: caddy:2-alpine
    restart: unless-stopped
    logging: *default-logging
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile:ro
      - caddy-data:/data
      - caddy-config:/config
    depends_on:
      - gluecron

  # Self-healing: watches every container that declares a healthcheck and
  # restarts any that goes unhealthy (a hung process that didn't crash).
  autoheal:
    image: willfarrell/autoheal:latest
    restart: unless-stopped
    logging: *default-logging
    environment:
      - AUTOHEAL_CONTAINER_LABEL=all
      - AUTOHEAL_INTERVAL=15
      - AUTOHEAL_START_PERIOD=60
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

volumes:
  pgdata:
  git-repos:
  caddy-data:
  caddy-config:
