# =============================================================================
# docker-compose.yml — production stack consumed by deploy.sh
# =============================================================================
#
# Complete Docker stack for production deployment:
#   - Next.js frontend (Node.js standalone server)
#   - PostgREST API (optional, for direct PostgreSQL access)
#
# Postgres itself runs on the host (the deploy workflow applies migrations
# via `psql -h 127.0.0.1`). Next.js can also run under PM2 on the host —
# this compose file provides a Docker alternative for the frontend.
#
# The host-side ports are written by the deploy workflow into $APP_DIR/.env.
# We interpolate from there with ${VAR:-default} so a manual
# `docker compose up` without the deploy script still works.
# =============================================================================

name: prod-app  # default project name; deploy.sh overrides with -p

services:
  # ── Next.js Frontend ──────────────────────────────────────────────────────────
  nextjs:
    build:
      context: ..
      dockerfile: deploy/Dockerfile.nextjs
      args:
        NEXT_PUBLIC_API_URL: ${NEXT_PUBLIC_API_URL:-http://localhost:3000}
        NODE_ENV: production
    container_name: prod-app-nextjs
    restart: unless-stopped
    ports:
      - "${NEXTJS_HOST_PORT:-3000}:3000"
    environment:
      NODE_ENV: production
      NEXT_PUBLIC_API_URL: ${NEXT_PUBLIC_API_URL:-http://localhost:3000}
      DATABASE_URL: ${DATABASE_URL}
      # Pass other env vars as needed
    healthcheck:
      test: ["CMD", "wget", "-qO-", "http://127.0.0.1:3000/"]
      interval: 10s
      timeout: 5s
      retries: 6
      start_period: 30s
    # Volume mount for hot-reload in development (comment out for production)
    # volumes:
    #   - ../src:/app/src:ro

  # ── PostgREST (optional direct PostgreSQL access) ────────────────────────────
  postgrest:
    image: postgrest/postgrest:latest
    container_name: prod-app-postgrest
    restart: unless-stopped
    # The host port is dynamic. The container always listens on 3000.
    ports:
      - "${POSTGREST_HOST_PORT:-3011}:3000"
    environment:
      PGRST_DB_URI: ${PGRST_DB_URI}
      PGRST_DB_ANON_ROLE: ${PGRST_DB_ANON_ROLE:-anon}
      PGRST_DB_SCHEMA: ${PGRST_DB_SCHEMA:-public}
      PGRST_SERVER_PORT: 3000
      # Optional: tighten CORS for your real domain
      PGRST_DB_TXN_END: "commit-allow-overwrite"
    depends_on:
      nextjs:
        condition: service_healthy
    # Healthcheck lets `docker compose ps` show healthy state.
    healthcheck:
      test: ["CMD", "wget", "-qO-", "http://127.0.0.1:3000/"]
      interval: 10s
      timeout: 3s
      retries: 6

