# AIWoiz — DEV stack (aiworkfllow.com)
#
# Run from this directory on the VPS:
#   cd /opt/aiw/dev && 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/aiworkfllow/).

name: aiw-dev

services:
  postgres:
    image: postgres:16-alpine
    container_name: aiw-dev-postgres
    # Mirrors prod: pg_stat_statements for per-query stats. After recreate, run
    # once:  docker exec aiw-dev-postgres psql -U aiw_voice -d aiw_voice_dev \
    #          -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_dev
    volumes:
      - pgdata:/var/lib/postgresql/data
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -U aiw_voice -d aiw_voice_dev']
      interval: 5s
      timeout: 5s
      retries: 10
    restart: unless-stopped
    networks: [aiw-dev-net]

  # One-shot service that runs Drizzle migrations and exits. The api service
  # waits for this to complete successfully before starting.
  migrate:
    build:
      context: ${AIW_SRC:-../..}/aiw-voice-backend
    container_name: aiw-dev-migrate
    command: ['node', 'dist/lib/db/migrate.js']
    environment:
      DATABASE_URL: postgresql://aiw_voice:${POSTGRES_PASSWORD}@postgres:5432/aiw_voice_dev
    depends_on:
      postgres:
        condition: service_healthy
    restart: 'no'
    networks: [aiw-dev-net]

  api:
    build:
      context: ${AIW_SRC:-../..}/aiw-voice-backend
    image: aiw-dev-api
    container_name: aiw-dev-api
    ports:
      - '127.0.0.1:3010:3000'
    env_file:
      - .env
    environment:
      # Wired from the env file too, but pinned here as defense-in-depth so
      # someone editing .env can't accidentally point dev at the wrong DB.
      DATABASE_URL: postgresql://aiw_voice:${POSTGRES_PASSWORD}@postgres:5432/aiw_voice_dev
      PORT: '3000'
    volumes:
      - ./storage:/app/storage
    depends_on:
      postgres:
        condition: service_healthy
      migrate:
        condition: service_completed_successfully
    restart: unless-stopped
    networks: [aiw-dev-net]

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

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

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

  # Next.js marketing site (apex aiworkfllow.com). Served by the Next
  # standalone Node server on container :3001 → host 127.0.0.1:5175.
  # cPanel nginx user-include at users/aiworkfllow/aiworkfllow.com/proxy.conf
  # proxies the apex to this port.
  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 this env's .env + `docker compose build marketing && up -d marketing`.
      context: ${AIW_SRC:-../..}/${MARKETING_PKG:-site-v0}
      args:
        MARKETING_MODE: staging   # → bakes <pkg>/.env.staging at build time
    image: aiw-dev-marketing
    container_name: aiw-dev-marketing
    ports:
      - '127.0.0.1:5175:3001'
    environment:
      # Server-only (NOT NEXT_PUBLIC): SSR fetchPlans reaches the API over the
      # internal network instead of hairpinning through public DNS. The visitor
      # IP is still forwarded via X-Forwarded-For so geo/currency stays correct.
      INTERNAL_API_BASE_URL: http://api:3000
    depends_on:
      - api
    restart: unless-stopped
    networks: [aiw-dev-net]

networks:
  aiw-dev-net:
    name: aiw-dev-net

volumes:
  pgdata:
    name: aiw-dev-pgdata
