# ============================================
# BraceIQMed - Docker Compose Configuration
# ============================================
# Usage:
#   docker compose build       # Build all images
#   docker compose up -d       # Start all services
#   docker compose logs -f     # View logs
#   docker compose down        # Stop all services
# ============================================

version: '3.8'

services:
  # ============================================
  # Frontend - React + nginx
  # ============================================
  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
    image: braceiqmed-frontend:latest
    container_name: braceiqmed-frontend
    restart: unless-stopped
    ports:
      - "80:80"
    depends_on:
      api:
        condition: service_healthy
    networks:
      - braceiqmed-net
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:80/health"]
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 10s
    logging:
      driver: "json-file"
      options:
        max-size: "50m"
        max-file: "5"

  # ============================================
  # API - Node.js Express Server
  # ============================================
  api:
    build:
      context: ./api
      dockerfile: Dockerfile
    image: braceiqmed-api:latest
    container_name: braceiqmed-api
    restart: unless-stopped
    environment:
      - NODE_ENV=production
      - PORT=3002
      - BRACE_GENERATOR_URL=http://brace-generator:8002
      - DATA_DIR=/app/data
      - DB_PATH=/app/data/braceflow.db
    volumes:
      - braceiqmed-data:/app/data
    depends_on:
      brace-generator:
        condition: service_healthy
    networks:
      - braceiqmed-net
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:3002/api/health"]
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 10s
    logging:
      driver: "json-file"
      options:
        max-size: "50m"
        max-file: "5"

  # ============================================
  # Brace Generator - FastAPI + PyTorch (CPU)
  # ============================================
  brace-generator:
    build:
      context: .
      dockerfile: brace-generator/Dockerfile
    image: braceiqmed-brace-generator:latest
    container_name: braceiqmed-brace-generator
    restart: unless-stopped
    environment:
      - HOST=0.0.0.0
      - PORT=8002
      - DEVICE=cpu
      - MODEL=scoliovis
      - TEMP_DIR=/tmp/brace_generator
      - CORS_ORIGINS=*
    volumes:
      - braceiqmed-data:/app/data
      - braceiqmed-models:/app/models
    networks:
      - braceiqmed-net
    deploy:
      resources:
        limits:
          memory: 8G
        reservations:
          memory: 4G
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8002/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s
    logging:
      driver: "json-file"
      options:
        max-size: "50m"
        max-file: "5"

# ============================================
# Networks
# ============================================
networks:
  braceiqmed-net:
    driver: bridge
    name: braceiqmed-net

# ============================================
# Volumes
# ============================================
volumes:
  # Persistent data storage (SQLite, uploads, outputs)
  braceiqmed-data:
    driver: local
    name: braceiqmed-data

  # Model storage (ScolioVis weights)
  braceiqmed-models:
    driver: local
    name: braceiqmed-models
