version: '3.8'

# Auto-generated docker-compose.yml for {{project_name}}
# Generated by `mecha10 init` - customize as needed
#
# Isomorphic Run Target Profiles:
#
#   Robot (hardware control):
#     docker-compose --profile robot up
#
#   Remote (cloud/edge compute):
#     docker-compose --profile remote up
#
#   Split deployment (robot + remote):
#     docker-compose --profile robot --profile remote up
#
#   Development (all nodes):
#     docker-compose --profile dev up
#
# Configuration via .env file (copy .env.example)

services:
  # ============================================================================
  # Robot: Hardware control (drivers + low-latency)
  # ============================================================================
  robot:
    profiles: ["robot"]
    build:
      context: {{build_context}}
      dockerfile: {{dockerfile_path}}
      target: production-robot
      args:
        RUN_TARGET: robot
    image: ${DOCKER_IMAGE:-{{project_name}}}:robot-${DOCKER_TAG:-latest}
    container_name: ${COMPOSE_PROJECT_NAME:-{{project_name}}}-robot

    environment:
      # Which config to load
      - MECHA10_ENVIRONMENT=${MECHA10_ENVIRONMENT:-production}

      # Infrastructure overrides (optional - for remote/SaaS)
      - MECHA10_REDIS_URL=${MECHA10_REDIS_URL:-}
      - MECHA10_POSTGRES_HOST=${MECHA10_POSTGRES_HOST:-}
      - MECHA10_POSTGRES_PASSWORD=${MECHA10_POSTGRES_PASSWORD:-}

      # Resource behavior
      - ALLOW_DEGRADED_MODE=${ALLOW_DEGRADED_MODE:-true}
      - REQUIRE_ALL_DEVICES=${REQUIRE_ALL_DEVICES:-false}

      # Logging
      - RUST_LOG=${RUST_LOG:-info}

    # Minimal volumes (production)
    volumes:
      - ${LOG_PATH:-./logs/robot}:/var/log/{{project_name}}

    # Hardware device mapping (configure via .env)
    devices:
      - ${CAMERA_DEVICE:-/dev/null}:/dev/video0
      - ${LIDAR_DEVICE:-/dev/null}:/dev/ttyUSB0
      - ${MOTOR_DEVICE:-/dev/null}:/dev/ttyACM0

    # Hardware capabilities (more secure than privileged)
    cap_add:
      - SYS_RAWIO  # Direct hardware access

    networks:
      - mecha10-network

    restart: always

    # Resource limits (adjust for robot hardware)
    deploy:
      resources:
        limits:
          cpus: ${ROBOT_CPU_LIMIT:-2.0}
          memory: ${ROBOT_MEMORY_LIMIT:-512M}

  # ============================================================================
  # Remote: Cloud/Edge compute (planning, ML inference)
  # ============================================================================
  remote:
    profiles: ["remote"]
    build:
      context: {{build_context}}
      dockerfile: {{dockerfile_path}}
      target: production-remote
      args:
        RUN_TARGET: remote
        ENABLE_GPU: ${GPU_ENABLED:-false}
        GPU_VENDOR: ${GPU_VENDOR:-nvidia}
    image: ${DOCKER_IMAGE:-{{project_name}}}:remote-${DOCKER_TAG:-latest}
    container_name: ${COMPOSE_PROJECT_NAME:-{{project_name}}}-remote

    environment:
      # Which config to load
      - MECHA10_ENVIRONMENT=${MECHA10_ENVIRONMENT:-production}

      # Infrastructure (required for remote)
      - MECHA10_REDIS_URL=${MECHA10_REDIS_URL:-}
      - MECHA10_POSTGRES_HOST=${MECHA10_POSTGRES_HOST:-}
      - MECHA10_POSTGRES_PASSWORD=${MECHA10_POSTGRES_PASSWORD:-}

      # GPU configuration
      - GPU_ENABLED=${GPU_ENABLED:-false}
      - CUDA_VISIBLE_DEVICES=${GPU_DEVICE_IDS:-0}
      - NVIDIA_DRIVER_CAPABILITIES=${NVIDIA_DRIVER_CAPABILITIES:-compute,utility}

      # Storage paths
      - MODEL_CACHE_PATH=${MODEL_CACHE_PATH:-/var/cache/mecha10/models}
      - DATA_PATH=${DATA_PATH:-/var/cache/mecha10/data}

      # Resource behavior
      - ALLOW_DEGRADED_MODE=${ALLOW_DEGRADED_MODE:-true}

      # Logging
      - RUST_LOG=${RUST_LOG:-info}

    # Volumes for models and data
    volumes:
      - ${LOG_PATH:-./logs/remote}:/var/log/{{project_name}}
      - ${MODEL_CACHE_PATH:-./cache/models}:/var/cache/mecha10/models
      - ${DATA_PATH:-./cache/data}:/var/cache/mecha10/data

    networks:
      - mecha10-network

    restart: always

    # Resource limits with GPU support
    deploy:
      resources:
        limits:
          cpus: ${REMOTE_CPU_LIMIT:-4.0}
          memory: ${REMOTE_MEMORY_LIMIT:-2G}
        # GPU reservation (NVIDIA)
        reservations:
          devices:
            - driver: nvidia
              count: ${GPU_COUNT:-all}
              capabilities: [gpu]

  # ============================================================================
  # Development: All nodes with hot reload
  # ============================================================================
  dev:
    profiles: ["dev"]
    build:
      context: {{build_context}}
      dockerfile: {{dockerfile_path}}
      target: development
    container_name: ${COMPOSE_PROJECT_NAME:-{{project_name}}}-dev

    environment:
      # Which config to load
      - MECHA10_ENVIRONMENT=${MECHA10_ENVIRONMENT:-development}

      # Infrastructure overrides
      - MECHA10_REDIS_URL=${MECHA10_REDIS_URL:-}
      - MECHA10_POSTGRES_HOST=${MECHA10_POSTGRES_HOST:-}
      - MECHA10_POSTGRES_PASSWORD=${MECHA10_POSTGRES_PASSWORD:-}

      # Logging
      - RUST_LOG=${RUST_LOG:-debug}

      # Hot reload control
      - CARGO_WATCH_ENABLED=${CARGO_WATCH_ENABLED:-true}

    # Mount source code for hot reload
    volumes:
{{#if is_monorepo}}
      # Framework packages (read-only)
      - ./packages:/workspace/packages:ro
{{/if}}
      # Project source (editable)
      - {{project_path}}/src:/workspace/{{project_path}}/src
      - {{project_path}}/drivers:/workspace/{{project_path}}/drivers
      - {{project_path}}/nodes:/workspace/{{project_path}}/nodes
      - {{project_path}}/embedded:/workspace/{{project_path}}/embedded
      - {{project_path}}/build.rs:/workspace/{{project_path}}/build.rs
      - {{project_path}}/mecha10.json:/workspace/{{project_path}}/mecha10.json
      - {{project_path}}/Cargo.toml:/workspace/{{project_path}}/Cargo.toml
      # Logs
      - {{project_path}}/logs:/workspace/{{project_path}}/logs
      # Cargo cache (speeds up rebuilds)
      - cargo-registry:/usr/local/cargo/registry
      - cargo-git:/usr/local/cargo/git
      - cargo-target:/workspace/target

    networks:
      - mecha10-network

    restart: unless-stopped

    # Hot reload command
    command: >
      bash -c "
        if [ \"$${CARGO_WATCH_ENABLED}\" = \"true\" ]; then
          echo '🔥 Hot reload enabled - watching for changes...'
          cargo watch -x 'run -- run'
        else
          cargo run -- run
        fi
      "

# ============================================================================
# Networks
# ============================================================================
networks:
  mecha10-network:
    # Development: Connect to shared infrastructure
    # Production: Can be external or isolated
    external: ${NETWORK_EXTERNAL:-true}
    name: ${NETWORK_NAME:-mecha10_mecha10-network}

# ============================================================================
# Volumes
# ============================================================================
volumes:
  # Cargo cache (development only)
  cargo-registry:
  cargo-git:
  cargo-target:
