# Secrets come from .env (auto-loaded by Compose) — never hard-code credentials here.
# Copy .env.example to .env and fill in values before running.
services:
  db:
    image: postgres:16-alpine
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-game}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB:-star_wars_grid}
    volumes:
      - ./data/postgres:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-game} -d ${POSTGRES_DB:-star_wars_grid}"]
      interval: 3s
      timeout: 5s
      retries: 15
      start_period: 5s

  users_db:
    image: postgres:16-alpine
    environment:
      POSTGRES_USER: ${POSTGRES_USERS_USER:-users}
      POSTGRES_PASSWORD: ${POSTGRES_USERS_PASSWORD}
      POSTGRES_DB: ${POSTGRES_USERS_DB:-star_wars_users}
    volumes:
      - ./data/postgres_users:/var/lib/postgresql/data
    ports:
      - "5433:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USERS_USER:-users} -d ${POSTGRES_USERS_DB:-star_wars_users}"]
      interval: 3s
      timeout: 5s
      retries: 15
      start_period: 5s

  mailpit:
    image: axllent/mailpit:latest
    ports:
      - "1025:1025"   # SMTP
      - "8025:8025"   # Web UI (open http://localhost:8025 to read emails)
    environment:
      MP_MAX_MESSAGES: 500
      MP_SMTP_AUTH_ACCEPT_ANY: 1
      MP_SMTP_AUTH_ALLOW_INSECURE: 1

  app:
    build: .
    ports:
      - "${PORT:-8080}:8080"
    environment:
      DATABASE_URL: "postgres://${POSTGRES_USER:-game}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB:-star_wars_grid}"
      USERS_DATABASE_URL: "postgres://${POSTGRES_USERS_USER:-users}:${POSTGRES_USERS_PASSWORD}@users_db:5432/${POSTGRES_USERS_DB:-star_wars_users}"
      JWT_SECRET: ${JWT_SECRET}
      ADMIN_PASSWORD: ${ADMIN_PASSWORD}
      PORT: "${PORT:-8080}"
      CONFIG_FILE_PATH: /app/config/game.settings.json
      SMTP_HOST: ${SMTP_HOST:-mailpit}
      SMTP_PORT: ${SMTP_PORT:-1025}
      SMTP_SECURE: ${SMTP_SECURE:-false}
      SMTP_USER: ${SMTP_USER:-}
      SMTP_PASS: ${SMTP_PASS:-}
      SMTP_FROM: ${SMTP_FROM:-Star Wars Wild Space <noreply@wildspace.local>}
      APP_URL: ${APP_URL:-http://localhost:8080}
    volumes:
      - ./config:/app/config
    depends_on:
      db:
        condition: service_healthy
      users_db:
        condition: service_healthy
      mailpit:
        condition: service_started