# Docker Compose for {{project_name}}
#
# Services for local development and deployment
#
# Usage:
#   docker compose up -d              # Start all services
#   docker compose up -d redis        # Start only Redis
#   docker compose down               # Stop and remove containers
#   docker compose logs -f redis      # View logs

version: '3.8'

services:
  # Redis - Message broker for Mecha10 pub/sub
  redis:
    image: redis:7-alpine
    container_name: {{project_name}}-redis
    ports:
      - '${REDIS_PORT:-6380}:6379'
    command: redis-server --appendonly yes --maxmemory 256mb --maxmemory-policy allkeys-lru
    volumes:
      - redis-data:/data
    healthcheck:
      test: ['CMD', 'redis-cli', 'ping']
      interval: 10s
      timeout: 3s
      retries: 5
      start_period: 5s
    networks:
      - {{project_name}}-network
    restart: unless-stopped

  # PostgreSQL - Optional database for remote nodes
  postgres:
    image: postgres:16-alpine
    container_name: {{project_name}}-postgres
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-robot}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-robot_dev_password}
      POSTGRES_DB: ${POSTGRES_DB:-{{project_name}}}
    ports:
      - '${POSTGRES_PORT:-5432}:5432'
    volumes:
      - postgres-data:/var/lib/postgresql/data
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -U ${POSTGRES_USER:-robot}']
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s
    networks:
      - {{project_name}}-network
    restart: unless-stopped
    profiles:
      - full

  # Godot Simulation - Headless with Xvfb for camera rendering
  simulation:
    build:
      context: .
      dockerfile: simulation/Dockerfile
    container_name: {{project_name}}-simulation
    environment:
      # Framework path for monorepo development
      MECHA10_FRAMEWORK_PATH: ${MECHA10_FRAMEWORK_PATH:-/mecha10}
      # Redis connection (internal Docker network)
      REDIS_URL: ${REDIS_URL_INTERNAL:-redis://redis:6379}
      # Display for Xvfb
      DISPLAY: ':99'
    ports:
      # Simulation networking ports from configs/dev/simulation/config.json
      - '${SIM_PROTOCOL_PORT:-11008}:11008'
      - '${SIM_CAMERA_PORT:-11009}:11009'
      # Note: Port 11010 (WebRTC signaling) is handled by simulation-bridge node, not Godot
    volumes:
      # Mount simulation assets (read-only)
      # Resolution order:
      #   1. MECHA10_FRAMEWORK_PATH (monorepo development)
      #   2. ~/.mecha10/simulation/current (cached from GitHub releases)
      - ${MECHA10_SIMULATION_PATH:-${HOME}/.mecha10/simulation/current}:/mecha10:ro
      # Mount project configs
      - ./configs:/project/configs:ro
      # Mount local simulation assets
      - ./simulation:/project/simulation:ro
    networks:
      - {{project_name}}-network
    depends_on:
      redis:
        condition: service_healthy
    # Don't auto-restart - simulation is managed by mecha10 dev
    restart: 'no'
    # Simulation runs on-demand, not by default
    profiles:
      - simulation

volumes:
  redis-data:
    name: {{project_name}}_redis_data
  postgres-data:
    name: {{project_name}}_postgres_data

networks:
  {{project_name}}-network:
    name: {{project_name}}_network
    driver: bridge
