# ==============================================================================
# RDM RSP API - Docker Compose Configuration
# ==============================================================================
# Production-ready configuration for Linux deployment
#
# Deployment Info:
#   URL:        https://rdmapi.blouzatech.ng
#   Path:       /var/www/eirs-projects/rdmapi.blouzatech.ng
#   Git Repo:   https://gitlab.com/blouza-tech/rdm-rsp-api.git
#
# Usage:
#   Development:  docker-compose up -d
#   Production:   docker-compose -f docker-compose.yml up -d
#   Logs:         docker-compose logs -f api
#   Stop:         docker-compose down
#   Rebuild:      docker-compose up -d --build
# ==============================================================================

services:
  # ============================================================================
  # RDM RSP API
  # ============================================================================
  api:
    # --------------------------------------------------------------------------
    # Build configuration
    # --------------------------------------------------------------------------
    build:
      context: .
      dockerfile: Dockerfile

    # --------------------------------------------------------------------------
    # Container naming
    # --------------------------------------------------------------------------
    image: rdm-rsp-api:latest
    container_name: rdm-rsp-api
    hostname: rsp-api

    # --------------------------------------------------------------------------
    # Restart policy
    # - always: Restart container if it stops for any reason
    # - unless-stopped: Same as always, but respects manual stops
    # --------------------------------------------------------------------------
    restart: unless-stopped

    # --------------------------------------------------------------------------
    # Port mapping
    # Host:Container - External port maps to container port 8080
    # nginx proxies https://rdmapi.blouzatech.ng to this port
    # --------------------------------------------------------------------------
    ports:
      - "${API_PORT:-8085}:8080"

    # --------------------------------------------------------------------------
    # Environment variables
    # Loaded from .env file and can be overridden
    # --------------------------------------------------------------------------
    env_file:
      - .env

    # --------------------------------------------------------------------------
    # Environment variable definitions
    # These override any values in .env file
    # --------------------------------------------------------------------------
    environment:
      # ASP.NET Core settings
      - ASPNETCORE_ENVIRONMENT=${ASPNETCORE_ENVIRONMENT:-Production}
      - ASPNETCORE_URLS=http://+:8080

      # Timezone (West Africa Time)
      - TZ=${TZ:-Africa/Lagos}

      # Database Connection String
      - ConnectionStrings__DefaultConnection=${DB_CONNECTION}

    # --------------------------------------------------------------------------
    # Volume mounts for data persistence
    # Left side = host path, Right side = container path
    # --------------------------------------------------------------------------
    volumes:
      # Application logs - for debugging and monitoring
      - rsp-logs:/app/logs

    # --------------------------------------------------------------------------
    # Health check configuration
    # Verifies API is responding via Swagger endpoint
    # --------------------------------------------------------------------------
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8080/swagger/index.html"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 60s

    # --------------------------------------------------------------------------
    # Resource limits
    # Adjust based on your server capacity
    # --------------------------------------------------------------------------
    deploy:
      resources:
        limits:
          cpus: '2.0'
          memory: 2G
        reservations:
          cpus: '0.5'
          memory: 512M

    # --------------------------------------------------------------------------
    # Logging configuration
    # Uses json-file driver with rotation
    # --------------------------------------------------------------------------
    logging:
      driver: "json-file"
      options:
        max-size: "50m"
        max-file: "5"

    # --------------------------------------------------------------------------
    # Network configuration
    # --------------------------------------------------------------------------
    networks:
      - rsp-network

    # --------------------------------------------------------------------------
    # DNS configuration for external database access
    # Ensure container can resolve external SQL Server
    # --------------------------------------------------------------------------
    dns:
      - 8.8.8.8
      - 8.8.4.4

    # --------------------------------------------------------------------------
    # Extra hosts (optional)
    # Add custom hostname mappings if needed
    # --------------------------------------------------------------------------
    # extra_hosts:
    #   - "sqlserver:your-server-ip"

    # --------------------------------------------------------------------------
    # Security options
    # --------------------------------------------------------------------------
    security_opt:
      - no-new-privileges:true

    # --------------------------------------------------------------------------
    # Temporary filesystems for runtime data
    # --------------------------------------------------------------------------
    tmpfs:
      - /tmp:size=100M

# ==============================================================================
# Named volumes for data persistence
# ==============================================================================
volumes:
  # Application logs storage
  rsp-logs:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: ${LOGS_VOLUME_PATH:-./data/logs}

# ==============================================================================
# Network configuration
# ==============================================================================
networks:
  rsp-network:
    driver: bridge
    # Let Docker auto-assign subnet to avoid conflicts with other projects
