version: '3.8'

# =============================================================================
# ERP-SUITE: CONSTRUCCION - Production Docker Compose
# =============================================================================
# Vertical: Construccion (35% completado)
# Puerto Frontend: 3020 | Puerto Backend: 3021
# Schemas BD: construccion (7 sub-schemas, 110 tablas)
# Depende de: auth.*, core.*, inventory.* (erp-core)
# =============================================================================

services:
  # ===========================================================================
  # BACKEND API
  # ===========================================================================
  backend:
    image: ${DOCKER_REGISTRY:-72.60.226.4:5000}/erp-construccion-backend:${VERSION:-latest}
    container_name: erp-construccion-backend
    restart: unless-stopped
    ports:
      - "3021:3021"
    environment:
      - NODE_ENV=production
      - PORT=3021
    env_file:
      - ./backend/.env.production
    volumes:
      - construccion-logs:/var/log/construccion
      - construccion-uploads:/app/uploads
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3021/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    networks:
      - erp-network
      - isem-network
    depends_on:
      - redis
    deploy:
      resources:
        limits:
          cpus: '1'
          memory: 512M
        reservations:
          cpus: '0.25'
          memory: 256M
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  # ===========================================================================
  # FRONTEND WEB
  # ===========================================================================
  frontend:
    image: ${DOCKER_REGISTRY:-72.60.226.4:5000}/erp-construccion-frontend:${VERSION:-latest}
    container_name: erp-construccion-frontend
    restart: unless-stopped
    ports:
      - "3020:80"
    depends_on:
      backend:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:80"]
      interval: 30s
      timeout: 10s
      retries: 3
    networks:
      - erp-network
    deploy:
      resources:
        limits:
          cpus: '0.5'
          memory: 128M
    logging:
      driver: "json-file"
      options:
        max-size: "5m"
        max-file: "2"

  # ===========================================================================
  # REDIS (Cache + Sessions + Queue)
  # ===========================================================================
  redis:
    image: redis:7-alpine
    container_name: erp-construccion-redis
    restart: unless-stopped
    ports:
      - "6380:6379"
    command: redis-server --appendonly yes --maxmemory 256mb --maxmemory-policy allkeys-lru
    volumes:
      - construccion-redis:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 3
    networks:
      - erp-network
    deploy:
      resources:
        limits:
          cpus: '0.5'
          memory: 256M

# =============================================================================
# VOLUMES
# =============================================================================
volumes:
  construccion-logs:
    driver: local
  construccion-uploads:
    driver: local
  construccion-redis:
    driver: local

# =============================================================================
# NETWORKS
# =============================================================================
networks:
  erp-network:
    driver: bridge
  isem-network:
    external: true
    name: isem-network
