# Docker Compose - ERP Construccion
# Version: 1.0
# Ambiente: Development

version: '3.8'

services:
  # ==========================================================================
  # DATABASE - PostgreSQL con PostGIS
  # ==========================================================================
  db:
    image: postgis/postgis:15-3.3-alpine
    container_name: construccion-db
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${DB_USER:-construccion}
      POSTGRES_PASSWORD: ${DB_PASSWORD:-construccion_dev_2024}
      POSTGRES_DB: ${DB_NAME:-erp_construccion}
      PGDATA: /var/lib/postgresql/data/pgdata
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./database/init-scripts:/docker-entrypoint-initdb.d:ro
    ports:
      - "${DB_PORT:-5433}:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${DB_USER:-construccion} -d ${DB_NAME:-erp_construccion}"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - construccion-network

  # ==========================================================================
  # REDIS - Cache y Colas
  # ==========================================================================
  redis:
    image: redis:7-alpine
    container_name: construccion-redis
    restart: unless-stopped
    command: redis-server --appendonly yes --requirepass ${REDIS_PASSWORD:-redis_dev_2024}
    volumes:
      - redis_data:/data
    ports:
      - "${REDIS_PORT:-6379}:6379"
    healthcheck:
      test: ["CMD", "redis-cli", "-a", "${REDIS_PASSWORD:-redis_dev_2024}", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - construccion-network

  # ==========================================================================
  # BACKEND - API Node.js + Express
  # ==========================================================================
  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
      target: ${BUILD_TARGET:-development}
    container_name: construccion-backend
    restart: unless-stopped
    environment:
      NODE_ENV: ${NODE_ENV:-development}
      APP_PORT: 3000
      API_VERSION: v1
      # Database
      DB_HOST: db
      DB_PORT: 5432
      DB_USER: ${DB_USER:-construccion}
      DB_PASSWORD: ${DB_PASSWORD:-construccion_dev_2024}
      DB_NAME: ${DB_NAME:-erp_construccion}
      # Redis
      REDIS_HOST: redis
      REDIS_PORT: 6379
      REDIS_PASSWORD: ${REDIS_PASSWORD:-redis_dev_2024}
      # JWT
      JWT_SECRET: ${JWT_SECRET:-your-super-secret-jwt-key-change-in-production}
      JWT_EXPIRES_IN: ${JWT_EXPIRES_IN:-1d}
      JWT_REFRESH_EXPIRES_IN: ${JWT_REFRESH_EXPIRES_IN:-7d}
      # CORS
      CORS_ORIGIN: ${CORS_ORIGIN:-http://localhost:5173,http://localhost:3001}
      CORS_CREDENTIALS: "true"
      # Logging
      LOG_LEVEL: ${LOG_LEVEL:-debug}
      LOG_FORMAT: dev
    volumes:
      - ./backend/src:/app/src:ro
      - ./backend/package.json:/app/package.json:ro
      - backend_node_modules:/app/node_modules
    ports:
      - "${BACKEND_PORT:-3000}:3000"
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    networks:
      - construccion-network

  # ==========================================================================
  # FRONTEND WEB - React + Vite
  # ==========================================================================
  frontend:
    build:
      context: ./frontend/web
      dockerfile: Dockerfile
      target: ${BUILD_TARGET:-development}
    container_name: construccion-frontend
    restart: unless-stopped
    environment:
      VITE_API_URL: ${VITE_API_URL:-http://localhost:3000/api/v1}
      VITE_WS_URL: ${VITE_WS_URL:-ws://localhost:3000}
    volumes:
      - ./frontend/web/src:/app/src:ro
      - ./frontend/web/public:/app/public:ro
      - ./frontend/web/index.html:/app/index.html:ro
      - frontend_node_modules:/app/node_modules
    ports:
      - "${FRONTEND_PORT:-5173}:5173"
    depends_on:
      - backend
    networks:
      - construccion-network

  # ==========================================================================
  # ADMINER - Database Management (Development Only)
  # ==========================================================================
  adminer:
    image: adminer:4-standalone
    container_name: construccion-adminer
    restart: unless-stopped
    environment:
      ADMINER_DEFAULT_SERVER: db
      ADMINER_DESIGN: pepa-linha-dark
    ports:
      - "${ADMINER_PORT:-8080}:8080"
    depends_on:
      - db
    profiles:
      - dev
    networks:
      - construccion-network

  # ==========================================================================
  # MAILHOG - Email Testing (Development Only)
  # ==========================================================================
  mailhog:
    image: mailhog/mailhog:latest
    container_name: construccion-mailhog
    restart: unless-stopped
    ports:
      - "${MAILHOG_SMTP_PORT:-1025}:1025"
      - "${MAILHOG_WEB_PORT:-8025}:8025"
    profiles:
      - dev
    networks:
      - construccion-network

# ==========================================================================
# VOLUMES
# ==========================================================================
volumes:
  postgres_data:
    driver: local
  redis_data:
    driver: local
  backend_node_modules:
    driver: local
  frontend_node_modules:
    driver: local

# ==========================================================================
# NETWORKS
# ==========================================================================
networks:
  construccion-network:
    driver: bridge
