# Production Docker Compose Configuration
# Usage: docker compose -f docker-compose.prod.yml up -d
#
# IMPORTANT: Database data is preserved in ./shared/db/data volume

services:
  db:
    image: postgres:17-alpine
    container_name: aurora_db
    restart: unless-stopped
    environment:
      - POSTGRES_USER=${DB_USER}
      - POSTGRES_PASSWORD=${DB_PASSWORD}
      - POSTGRES_DB=${DB_NAME}
    volumes:
      # Database data - persisted across container rebuilds
      - ./shared/db/data:/var/lib/postgresql/data
      - ./shared/db/log:/var/log/postgresql
    networks:
      - internal
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -U ${DB_USER} -d ${DB_NAME}" ]
      interval: 10s
      timeout: 5s
      retries: 5
    # Security: limit resources
    deploy:
      resources:
        limits:
          memory: 512M

  app:
    container_name: aurora_app
    restart: unless-stopped
    build:
      context: .
      dockerfile: Dockerfile
      target: production
    image: aurora-app:latest
    volumes:
      - ./bot/assets/graphics/items:/app/bot/assets/graphics/items
    ports:
      - "127.0.0.1:3000:3000"

    working_dir: /app
    environment:
      - NODE_ENV=production
      - HOST=0.0.0.0
      - DB_USER=${DB_USER}
      - DB_PASSWORD=${DB_PASSWORD}
      - DB_NAME=${DB_NAME}
      - DB_PORT=5432
      - DB_HOST=db
      - DISCORD_BOT_TOKEN=${DISCORD_BOT_TOKEN}
      - DISCORD_GUILD_ID=${DISCORD_GUILD_ID}
      - DISCORD_CLIENT_ID=${DISCORD_CLIENT_ID}
      - DATABASE_URL=postgresql://${DB_USER}:${DB_PASSWORD}@db:5432/${DB_NAME}
      - DISCORD_CLIENT_SECRET=${DISCORD_CLIENT_SECRET}
      - SESSION_SECRET=${SESSION_SECRET}
      - ADMIN_USER_IDS=${ADMIN_USER_IDS}
      - PANEL_BASE_URL=${PANEL_BASE_URL:-https://aurora.syntaxbullet.com}

    depends_on:
      db:
        condition: service_healthy

    networks:
      - internal
      - web
    # Security: limit resources
    deploy:
      resources:
        limits:
          memory: 1G
    # Logging configuration
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  studio:
    container_name: aurora_studio
    image: aurora-app:latest
    volumes:
      - ./bot/assets/graphics/items:/app/bot/assets/graphics/items
    restart: unless-stopped
    depends_on:
      db:
        condition: service_healthy
    ports:
      - "127.0.0.1:4983:4983"
    environment:
      - NODE_ENV=production
      - DB_USER=${DB_USER}
      - DB_PASSWORD=${DB_PASSWORD}
      - DB_NAME=${DB_NAME}
      - DB_PORT=5432
      - DB_HOST=db
      - DATABASE_URL=postgresql://${DB_USER}:${DB_PASSWORD}@db:5432/${DB_NAME}
      - DISCORD_CLIENT_SECRET=${DISCORD_CLIENT_SECRET}
      - SESSION_SECRET=${SESSION_SECRET}
      - ADMIN_USER_IDS=${ADMIN_USER_IDS}
      - PANEL_BASE_URL=${PANEL_BASE_URL:-https://aurora.syntaxbullet.com}
    networks:
      - internal
      - web
    command: bun run db:studio
    healthcheck:
      test: [ "CMD", "bun", "-e", "fetch('http://localhost:4983').then(r => process.exit(0)).catch(() => process.exit(1))" ]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 10s
    deploy:
      resources:
        limits:
          memory: 512M

networks:
  internal:
    driver: bridge
    internal: true # No external access - DB isolated
  web:
    driver: bridge # App accessible from host (via reverse proxy)
