version: '3.8'

services:
  # Main application
  tempering-machine:
    build:
      context: .
      dockerfile: Dockerfile
      args:
        BUILD_ENV: production
    container_name: tempering-machine
    restart: unless-stopped
    ports:
      - "8000:8000"
    environment:
      # Database configuration
      - TEMPERING_DATABASE_URL=sqlite:///data/tempering_machine.db
      
      # Serial/Modbus configuration  
      - TEMPERING_SERIAL__PORT=/dev/ttyUSB0
      - TEMPERING_SERIAL__BAUDRATE=9600
      - TEMPERING_SERIAL__TIMEOUT=2.0
      
      # Safety configuration
      - TEMPERING_SAFETY__MAX_TANK_TEMPERATURE=80.0
      - TEMPERING_SAFETY__MAX_NEUTRAL_CURRENT=16.0
      - TEMPERING_SAFETY__COMMUNICATION_TIMEOUT=5.0
      
      # Web configuration
      - TEMPERING_WEB__HOST=0.0.0.0
      - TEMPERING_WEB__PORT=8000
      - TEMPERING_WEB__CORS_ORIGINS=["http://localhost:3000", "http://localhost:8080"]
      
      # Application settings
      - TEMPERING_ENVIRONMENT=production
      - TEMPERING_LOG_LEVEL=INFO
      - TEMPERING_DEBUG=false
    volumes:
      - ./data:/app/data
      - ./logs:/app/logs
      - ./backups:/app/backups
      - ./config:/app/config
      # Mount serial device (adjust as needed)
      - /dev/ttyUSB0:/dev/ttyUSB0
    devices:
      - /dev/ttyUSB0:/dev/ttyUSB0
    networks:
      - tempering-network
    depends_on:
      - redis
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health/live"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s

  # Redis for message queuing and caching
  redis:
    image: redis:7-alpine
    container_name: tempering-redis
    restart: unless-stopped
    ports:
      - "6379:6379"
    volumes:
      - redis-data:/data
    networks:
      - tempering-network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 30s
      timeout: 10s
      retries: 3

  # Optional: PostgreSQL database (if not using SQLite)
  postgres:
    image: postgres:15-alpine
    container_name: tempering-postgres
    restart: unless-stopped
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_DB=tempering_machine
      - POSTGRES_USER=tempering
      - POSTGRES_PASSWORD=tempering123
    volumes:
      - postgres-data:/var/lib/postgresql/data
      - ./docker/postgres/init.sql:/docker-entrypoint-initdb.d/init.sql
    networks:
      - tempering-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U tempering"]
      interval: 30s
      timeout: 10s
      retries: 3
    profiles:
      - postgres

  # Optional: Grafana for monitoring dashboards
  grafana:
    image: grafana/grafana:latest
    container_name: tempering-grafana
    restart: unless-stopped
    ports:
      - "3001:3000"
    environment:
      - GF_SECURITY_ADMIN_PASSWORD=admin123
      - GF_USERS_ALLOW_SIGN_UP=false
    volumes:
      - grafana-data:/var/lib/grafana
      - ./docker/grafana/dashboards:/etc/grafana/provisioning/dashboards
      - ./docker/grafana/datasources:/etc/grafana/provisioning/datasources
    networks:
      - tempering-network
    profiles:
      - monitoring

  # Optional: Prometheus for metrics collection
  prometheus:
    image: prom/prometheus:latest
    container_name: tempering-prometheus
    restart: unless-stopped
    ports:
      - "9090:9090"
    volumes:
      - prometheus-data:/prometheus
      - ./docker/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
    networks:
      - tempering-network
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.console.libraries=/etc/prometheus/console_libraries'
      - '--web.console.templates=/etc/prometheus/consoles'
      - '--storage.tsdb.retention.time=200h'
      - '--web.enable-lifecycle'
    profiles:
      - monitoring

networks:
  tempering-network:
    driver: bridge

volumes:
  redis-data:
    driver: local
  postgres-data:
    driver: local
  grafana-data:
    driver: local
  prometheus-data:
    driver: local