version: '3.9'

# ═══════════════════════════════════════════════════════════
#  WordPlay — Docker Compose
#
#  JSON mode (default):
#    docker compose up
#
#  MySQL mode:
#    docker compose --profile mysql up
#
#  PostgreSQL mode:
#    docker compose --profile postgres up
# ═══════════════════════════════════════════════════════════

services:

  # ── App (JSON mode) ──────────────────────────────────────
  app:
    build: .
    image: wordplay:latest
    ports:
      - "${PORT:-3000}:3000"
    environment:
      PORT:         3000
      STORAGE_MODE: ${STORAGE_MODE:-json}
      CORS_ORIGIN:  ${CORS_ORIGIN:-*}
    volumes:
      - wordplay_data:/app/data
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "node", "-e", "require('http').get('http://localhost:3000/api/ping',r=>process.exit(r.statusCode===200?0:1)).on('error',()=>process.exit(1))"]
      interval: 30s
      timeout: 5s
      retries: 3

  # ── App (MySQL mode) ─────────────────────────────────────
  app-mysql:
    build: .
    image: wordplay:latest
    profiles: [mysql]
    ports:
      - "${PORT:-3000}:3000"
    environment:
      PORT:         3000
      STORAGE_MODE: mysql
      DB_HOST:      mysql
      DB_PORT:      3306
      DB_NAME:      wordplay
      DB_USER:      wordplay
      DB_PASS:      wordplay_secret
    depends_on:
      mysql:
        condition: service_healthy
    restart: unless-stopped

  # ── App (Postgres mode) ──────────────────────────────────
  app-postgres:
    build: .
    image: wordplay:latest
    profiles: [postgres]
    ports:
      - "${PORT:-3000}:3000"
    environment:
      PORT:         3000
      STORAGE_MODE: postgres
      DB_HOST:      postgres
      DB_PORT:      5432
      DB_NAME:      wordplay
      DB_USER:      wordplay
      DB_PASS:      wordplay_secret
    depends_on:
      postgres:
        condition: service_healthy
    restart: unless-stopped

  # ── MySQL ─────────────────────────────────────────────────
  mysql:
    image: mysql:8.0
    profiles: [mysql]
    environment:
      MYSQL_DATABASE:      wordplay
      MYSQL_USER:          wordplay
      MYSQL_PASSWORD:      wordplay_secret
      MYSQL_ROOT_PASSWORD: root_secret
    volumes:
      - mysql_data:/var/lib/mysql
      - ./schema.sql:/docker-entrypoint-initdb.d/schema.sql:ro
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "wordplay", "-pwordplay_secret"]
      interval: 10s
      timeout: 5s
      retries: 5
    restart: unless-stopped

  # ── PostgreSQL ────────────────────────────────────────────
  postgres:
    image: postgres:16-alpine
    profiles: [postgres]
    environment:
      POSTGRES_DB:       wordplay
      POSTGRES_USER:     wordplay
      POSTGRES_PASSWORD: wordplay_secret
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./schema.sql:/docker-entrypoint-initdb.d/schema.sql:ro
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U wordplay -d wordplay"]
      interval: 10s
      timeout: 5s
      retries: 5
    restart: unless-stopped

volumes:
  wordplay_data:
  mysql_data:
  postgres_data:
