# AIWoiz — PROD stack (aiandhumn.com)
#
# Run from this directory on the VPS:
#   cd /opt/aiw/prod && docker compose up -d --build
#
# All host ports bind to 127.0.0.1 only — public access happens via the
# cPanel-managed nginx vhosts that proxy to these loopback ports
# (see deploy/cpanel-nginx/aiandhumn/).

name: aiw-prod

services:
  postgres:
    image: postgres:16-alpine
    container_name: aiw-prod-postgres
    # pg_stat_statements = per-query timing/count stats for the perf runbook
    # (weekly: top-10 by mean_exec_time). Loading the library requires a
    # postgres restart (`docker compose up -d postgres` recreates); then run
    # once:  docker exec aiw-prod-postgres psql -U aiw_voice -d aiw_voice_prod \
    #          -c 'CREATE EXTENSION IF NOT EXISTS pg_stat_statements;'
    command: postgres -c shared_preload_libraries=pg_stat_statements
    environment:
      POSTGRES_USER: aiw_voice
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: aiw_voice_prod
    volumes:
      - pgdata:/var/lib/postgresql/data
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -U aiw_voice -d aiw_voice_prod']
      interval: 5s
      timeout: 5s
      retries: 10
    restart: unless-stopped
    networks: [aiw-prod-net]

  migrate:
    build:
      context: ${AIW_SRC:-../..}/aiw-voice-backend
    container_name: aiw-prod-migrate
    command: ['node', 'dist/lib/db/migrate.js']
    environment:
      DATABASE_URL: postgresql://aiw_voice:${POSTGRES_PASSWORD}@postgres:5432/aiw_voice_prod
    depends_on:
      postgres:
        condition: service_healthy
    restart: 'no'
    networks: [aiw-prod-net]

  api:
    build:
      context: ${AIW_SRC:-../..}/aiw-voice-backend
    image: aiw-prod-api
    container_name: aiw-prod-api
    ports:
      - '127.0.0.1:3020:3000'
    env_file:
      - .env
    environment:
      DATABASE_URL: postgresql://aiw_voice:${POSTGRES_PASSWORD}@postgres:5432/aiw_voice_prod
      PORT: '3000'
    volumes:
      - ./storage:/app/storage
    depends_on:
      postgres:
        condition: service_healthy
      migrate:
        condition: service_completed_successfully
    restart: unless-stopped
    networks: [aiw-prod-net]

  app:
    build:
      context: ${AIW_SRC:-../..}/aiw-voice-app
      args:
        VITE_MODE: production   # → uses aiw-voice-app/.env.production at build time
    container_name: aiw-prod-app
    ports:
      - '127.0.0.1:5183:80'
    restart: unless-stopped
    networks: [aiw-prod-net]

  widget:
    build:
      context: ${AIW_SRC:-../..}/aiw-voice-widget
    container_name: aiw-prod-widget
    ports:
      - '127.0.0.1:5184:80'
    restart: unless-stopped
    networks: [aiw-prod-net]

  # Boojee Cafe ordering SPA (cafe.aiandhumn.com). Static nginx; serves the app
  # + the vendored cafe widget bundle at /aiw-cafe-widget.js (same origin).
  # cPanel user-include at users/aiandhumn/cafe.aiandhumn.com/proxy.conf proxies
  # the subdomain to this loopback port. Paste the prod Boojee widget token into
  # boojee-cafe-app/.env.production before building (see that file).
  cafe:
    build:
      context: ${AIW_SRC:-../..}/boojee-cafe-app
      args:
        VITE_MODE: production   # → uses boojee-cafe-app/.env.production at build time
    container_name: aiw-prod-cafe
    ports:
      - '127.0.0.1:5186:80'
    restart: unless-stopped
    networks: [aiw-prod-net]

  # Next.js marketing site (apex aiandhumn.com). Container :3001 → host
  # 127.0.0.1:5185. DEFINED here for repo-readiness but DORMANT until the
  # Phase E cutover flips the apex nginx user-include from the SPA (:5183)
  # to this port AND the SPA is relocated to app.aiandhumn.com. Building /
  # starting this service early is harmless — nothing routes to :5185 until
  # that nginx flip.
  marketing:
    build:
      # Soft-launch split: MARKETING_PKG selects which marketing package is live —
      # site-v0 (waitlist, default) or beta-v1 (parked full product site). Flip it
      # in /opt/aiw/prod/.env + `docker compose build marketing && up -d marketing`.
      context: ${AIW_SRC:-../..}/${MARKETING_PKG:-site-v0}
      args:
        MARKETING_MODE: production   # → bakes <pkg>/.env.production at build time
    image: aiw-prod-marketing
    container_name: aiw-prod-marketing
    ports:
      - '127.0.0.1:5185:3001'
    environment:
      INTERNAL_API_BASE_URL: http://api:3000
    depends_on:
      - api
    restart: unless-stopped
    networks: [aiw-prod-net]

networks:
  aiw-prod-net:
    name: aiw-prod-net

volumes:
  pgdata:
    name: aiw-prod-pgdata
