# ==============================================================================
# EIRS Admin - Docker Compose Configuration
# ==============================================================================
# Production-ready configuration for Linux deployment
# Connects to external SQL Server database at 45.13.59.80
#
# Usage:
#   Start:    docker-compose up -d
#   Logs:     docker-compose logs -f eirs-admin
#   Stop:     docker-compose down
#   Rebuild:  docker-compose up -d --build
# ==============================================================================

services:
  # ============================================================================
  # EIRS Admin Web Application
  # ============================================================================
  eirs-admin:
    # --------------------------------------------------------------------------
    # Build configuration
    # --------------------------------------------------------------------------
    build:
      context: .
      dockerfile: Presentation/EIRS.Admin.Core/Dockerfile

    # --------------------------------------------------------------------------
    # Container naming
    # --------------------------------------------------------------------------
    image: eirs-admin:latest
    container_name: eirs-admin
    hostname: eirs-admin

    # --------------------------------------------------------------------------
    # Restart policy
    # --------------------------------------------------------------------------
    restart: unless-stopped

    # --------------------------------------------------------------------------
    # Port mapping
    # Host:Container - External port 8085 maps to container port 8080
    # Access at: http://45.13.59.80:8085
    # --------------------------------------------------------------------------
    ports:
      - "${API_PORT:-8085}:8080"

    # --------------------------------------------------------------------------
    # Environment variables from .env file
    # --------------------------------------------------------------------------
    env_file:
      - .env

    # --------------------------------------------------------------------------
    # Environment variable definitions
    # --------------------------------------------------------------------------
    environment:
      # ASP.NET Core settings
      - ASPNETCORE_ENVIRONMENT=${ASPNETCORE_ENVIRONMENT:-Production}
      - ASPNETCORE_URLS=http://+:8080

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

      # Database Connection Strings
      # Server: 45.13.59.80 (External SQL Server)
      - ConnectionStrings__DbEntities=${DB_EIRS_CONNECTION}
      - ConnectionStrings__ERASEntities=${DB_ERAS_CONNECTION}
      - ConnectionStrings__ERASDWEntities=${DB_ERASDW_CONNECTION}
      - ConnectionStrings__ErrorConnection=${DB_ERROR_CONNECTION}
      - ConnectionStrings__SessionConnection=${DB_SESSION_CONNECTION}

      # Application settings
      - AppSettings__Project=EIRS Automation
      - AppSettings__Company=EIRS

    # --------------------------------------------------------------------------
    # Volume mounts for data persistence
    # --------------------------------------------------------------------------
    volumes:
      # Application logs
      - eirs-logs:/app/logs

      # Data protection keys
      - eirs-keys:/app/keys

      # Temporary files (PDF/Excel generation)
      - eirs-temp:/app/temp

    # --------------------------------------------------------------------------
    # Health check configuration
    # --------------------------------------------------------------------------
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 60s

    # --------------------------------------------------------------------------
    # Resource limits
    # --------------------------------------------------------------------------
    deploy:
      resources:
        limits:
          cpus: '2.0'
          memory: 2G
        reservations:
          cpus: '0.5'
          memory: 512M

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

    # --------------------------------------------------------------------------
    # Network configuration
    # --------------------------------------------------------------------------
    networks:
      - eirs-network

    # --------------------------------------------------------------------------
    # DNS configuration for external database access
    # --------------------------------------------------------------------------
    dns:
      - 8.8.8.8
      - 8.8.4.4

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

    # --------------------------------------------------------------------------
    # Temporary filesystem
    # --------------------------------------------------------------------------
    tmpfs:
      - /tmp:size=100M

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

  eirs-keys:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: ${KEYS_VOLUME_PATH:-./data/keys}

  eirs-temp:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: ${TEMP_VOLUME_PATH:-./data/temp}

# ==============================================================================
# Network configuration
# ==============================================================================
networks:
  eirs-network:
    driver: bridge
