# TimeSafari Docker Compose Configuration
# Author: Matthew Raymer
# Description: Multi-environment Docker Compose setup for TimeSafari
#
# Usage:
#   Development: docker-compose up dev
#   Staging: docker-compose up staging
#   Production: docker-compose up production
#   Custom: BUILD_MODE=staging docker-compose up custom
#
# Environment Variables:
#   BUILD_MODE: development, staging, or production (default: production)
#   NODE_ENV: node environment (default: production)
#   VITE_PLATFORM: vite platform (default: web)
#   VITE_PWA_ENABLED: enable PWA (default: true)
#   VITE_DISABLE_PWA: disable PWA (default: false)
#   PORT: port to expose (default: 80 for production, 5173 for dev)
#   ENV_FILE: environment file to use (default: .env.production)
#
# See .env files for application-specific configuration
#   VITE_APP_SERVER: Application server URL
#   VITE_DEFAULT_ENDORSER_API_SERVER: Endorser API server URL

version: '3.8'

# Default values that can be overridden
x-defaults: &defaults
  build:
    context: .
    dockerfile: Dockerfile
    args:
      BUILD_MODE: ${BUILD_MODE:-production}
      NODE_ENV: ${NODE_ENV:-production}
      VITE_PLATFORM: ${VITE_PLATFORM:-web}
      VITE_PWA_ENABLED: ${VITE_PWA_ENABLED:-true}
      VITE_DISABLE_PWA: ${VITE_DISABLE_PWA:-false}
  restart: unless-stopped
  healthcheck:
    test: ["CMD", "curl", "-f", "http://localhost/health"]
    interval: 30s
    timeout: 10s
    retries: 3
    start_period: 40s

services:
  # Development service with hot reloading
  dev:
    <<: *defaults
    build:
      context: .
      dockerfile: Dockerfile
      target: development
      args:
        BUILD_MODE: development
        NODE_ENV: development
        VITE_PLATFORM: web
        VITE_PWA_ENABLED: true
        VITE_DISABLE_PWA: false
    ports:
      - "${DEV_PORT:-5173}:5173"
    volumes:
      - .:/app
      - /app/node_modules
    environment:
      - NODE_ENV=development
      - VITE_PLATFORM=web
      - VITE_PWA_ENABLED=true
      - VITE_DISABLE_PWA=false
    env_file:
      - ${DEV_ENV_FILE:-.env.development}
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:5173"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  # Staging service for testing
  staging:
    <<: *defaults
    build:
      context: .
      dockerfile: Dockerfile
      target: staging
      args:
        BUILD_MODE: staging
        NODE_ENV: staging
        VITE_PLATFORM: web
        VITE_PWA_ENABLED: true
        VITE_DISABLE_PWA: false
    ports:
      - "${STAGING_PORT:-8080}:80"
    environment:
      - NODE_ENV=staging
      - VITE_PLATFORM=web
      - VITE_PWA_ENABLED=true
      - VITE_DISABLE_PWA=false
    env_file:
      - ${STAGING_ENV_FILE:-.env.staging}

  # Production service
  production:
    <<: *defaults
    build:
      context: .
      dockerfile: Dockerfile
      target: production
      args:
        BUILD_MODE: production
        NODE_ENV: production
        VITE_PLATFORM: web
        VITE_PWA_ENABLED: true
        VITE_DISABLE_PWA: false
    ports:
      - "${PROD_PORT:-80}:80"
    environment:
      - NODE_ENV=production
      - VITE_PLATFORM=web
      - VITE_PWA_ENABLED=true
      - VITE_DISABLE_PWA=false
    env_file:
      - ${PROD_ENV_FILE:-.env.production}

  # Production service with SSL (requires certificates)
  production-ssl:
    <<: *defaults
    build:
      context: .
      dockerfile: Dockerfile
      target: production
      args:
        BUILD_MODE: production
        NODE_ENV: production
        VITE_PLATFORM: web
        VITE_PWA_ENABLED: true
        VITE_DISABLE_PWA: false
    ports:
      - "${SSL_PORT:-443}:443"
      - "${HTTP_PORT:-80}:80"
    environment:
      - NODE_ENV=production
      - VITE_PLATFORM=web
      - VITE_PWA_ENABLED=true
      - VITE_DISABLE_PWA=false
    env_file:
      - ${PROD_ENV_FILE:-.env.production}
    volumes:
      - ./ssl:/etc/nginx/ssl:ro
      - ./docker/nginx-ssl.conf:/etc/nginx/conf.d/default.conf:ro
    healthcheck:
      test: ["CMD", "curl", "-f", "https://localhost/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  # Custom service - configurable via environment variables
  custom:
    <<: *defaults
    build:
      context: .
      dockerfile: Dockerfile
      target: ${BUILD_TARGET:-production}
      args:
        BUILD_MODE: ${BUILD_MODE:-production}
        NODE_ENV: ${NODE_ENV:-production}
        VITE_PLATFORM: ${VITE_PLATFORM:-web}
        VITE_PWA_ENABLED: ${VITE_PWA_ENABLED:-true}
        VITE_DISABLE_PWA: ${VITE_DISABLE_PWA:-false}
    ports:
      - "${CUSTOM_PORT:-8080}:${CUSTOM_INTERNAL_PORT:-80}"
    environment:
      - NODE_ENV=${NODE_ENV:-production}
      - VITE_PLATFORM=${VITE_PLATFORM:-web}
      - VITE_PWA_ENABLED=${VITE_PWA_ENABLED:-true}
      - VITE_DISABLE_PWA=${VITE_DISABLE_PWA:-false}
    env_file:
      - ${CUSTOM_ENV_FILE:-.env.production}
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:${CUSTOM_INTERNAL_PORT:-80}/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  # Load balancer for production (optional)
  nginx-lb:
    image: nginx:alpine
    ports:
      - "${LB_PORT:-80}:80"
      - "${LB_SSL_PORT:-443}:443"
    volumes:
      - ./docker/nginx-lb.conf:/etc/nginx/nginx.conf:ro
      - ./ssl:/etc/nginx/ssl:ro
    depends_on:
      - production
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

networks:
  default:
    driver: bridge
    ipam:
      config:
        - subnet: 172.20.0.0/16 