# =============================================================================
# DOCKER COMPOSE - ERP CONSTRUCCION
# =============================================================================
# Ambiente: Desarrollo Local
# Version: 1.1.0
# Fecha: 2025-12-06
# Puertos: Rango 3100 (ver DEVENV-PORTS.md)
# =============================================================================

version: '3.8'

services:
  # ---------------------------------------------------------------------------
  # DATABASE: PostgreSQL 15 + PostGIS
  # Puerto: 5433 (diferenciado de erp-core:5432)
  # ---------------------------------------------------------------------------
  postgres:
    image: postgis/postgis:15-3.3-alpine
    container_name: erp_construccion_db
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${DB_USER:-erp_user}
      POSTGRES_PASSWORD: ${DB_PASSWORD:-erp_dev_password}
      POSTGRES_DB: ${DB_NAME:-erp_construccion}
    ports:
      - "${DB_PORT:-5433}:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./init-scripts:/docker-entrypoint-initdb.d:ro
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${DB_USER:-erp_user} -d ${DB_NAME:-erp_construccion}"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - erp_network

  # ---------------------------------------------------------------------------
  # CACHE: Redis
  # Puerto: 6380 (diferenciado de erp-core:6379)
  # ---------------------------------------------------------------------------
  redis:
    image: redis:7-alpine
    container_name: erp_construccion_redis
    restart: unless-stopped
    ports:
      - "${REDIS_PORT:-6380}:6379"
    volumes:
      - redis_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - erp_network

  # ---------------------------------------------------------------------------
  # STORAGE: MinIO (S3 Compatible)
  # Puertos: 9100/9101 (diferenciado de erp-core:9000/9001)
  # ---------------------------------------------------------------------------
  minio:
    image: minio/minio:latest
    container_name: erp_construccion_minio
    restart: unless-stopped
    environment:
      MINIO_ROOT_USER: ${MINIO_USER:-minioadmin}
      MINIO_ROOT_PASSWORD: ${MINIO_PASSWORD:-minioadmin}
    ports:
      - "${MINIO_PORT:-9100}:9000"
      - "${MINIO_CONSOLE_PORT:-9101}:9001"
    volumes:
      - minio_data:/data
    command: server /data --console-address ":9001"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
      interval: 30s
      timeout: 20s
      retries: 3
    networks:
      - erp_network

  # ---------------------------------------------------------------------------
  # BACKEND: Node.js API
  # Puerto: 3100 (diferenciado de erp-core:3000)
  # ---------------------------------------------------------------------------
  backend:
    build:
      context: ../../../../backend
      dockerfile: Dockerfile
      args:
        NODE_ENV: development
    container_name: erp_construccion_backend
    restart: unless-stopped
    environment:
      NODE_ENV: development
      PORT: 3100
      DATABASE_URL: postgresql://${DB_USER:-erp_user}:${DB_PASSWORD:-erp_dev_password}@postgres:5432/${DB_NAME:-erp_construccion}
      REDIS_URL: redis://redis:6379
      JWT_SECRET: ${JWT_SECRET:-dev-secret-change-in-production}
      JWT_EXPIRES_IN: ${JWT_EXPIRES_IN:-1d}
      S3_ENDPOINT: http://minio:9000
      S3_ACCESS_KEY: ${MINIO_USER:-minioadmin}
      S3_SECRET_KEY: ${MINIO_PASSWORD:-minioadmin}
      S3_BUCKET: ${S3_BUCKET:-erp-construccion}
    ports:
      - "${API_PORT:-3100}:3100"
    volumes:
      - ../../../../apps/backend:/app
      - /app/node_modules
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - erp_network

  # ---------------------------------------------------------------------------
  # FRONTEND: React/Vite
  # Puerto: 5174 (diferenciado de erp-core:5173)
  # ---------------------------------------------------------------------------
  frontend:
    build:
      context: ../../../../frontend/web
      dockerfile: Dockerfile
      args:
        NODE_ENV: development
    container_name: erp_construccion_frontend
    restart: unless-stopped
    environment:
      VITE_API_URL: http://localhost:${API_PORT:-3100}
    ports:
      - "${FRONTEND_PORT:-5174}:5173"
    volumes:
      - ../../../../apps/frontend:/app
      - /app/node_modules
    depends_on:
      - backend
    networks:
      - erp_network

  # ---------------------------------------------------------------------------
  # ADMINER: Database UI (opcional)
  # Puerto: 8081 (diferenciado)
  # ---------------------------------------------------------------------------
  adminer:
    image: adminer:latest
    container_name: erp_construccion_adminer
    restart: unless-stopped
    ports:
      - "${ADMINER_PORT:-8081}:8080"
    environment:
      ADMINER_DEFAULT_SERVER: postgres
    depends_on:
      - postgres
    networks:
      - erp_network
    profiles:
      - tools

# =============================================================================
# VOLUMES
# =============================================================================
volumes:
  postgres_data:
    name: erp_construccion_postgres_data
  redis_data:
    name: erp_construccion_redis_data
  minio_data:
    name: erp_construccion_minio_data

# =============================================================================
# NETWORKS
# =============================================================================
networks:
  erp_network:
    name: erp_construccion_network
    driver: bridge

# =============================================================================
# USAGE
# =============================================================================
# Iniciar todos los servicios:
#   docker-compose up -d
#
# Iniciar con herramientas (adminer):
#   docker-compose --profile tools up -d
#
# Ver logs:
#   docker-compose logs -f backend
#
# Detener:
#   docker-compose down
#
# Reset completo:
#   docker-compose down -v
# =============================================================================
