# Docker Compose pour l'écosystème O-Red complet
version: '3.8'

services:
  # API centrale O-Red
  ored-api:
    build: .
    container_name: ored-central-api
    ports:
      - "8000:8000"
    environment:
      - DATABASE_URL=postgresql+asyncpg://ored:ored_secure_password@postgres:5432/ored_central
      - REDIS_URL=redis://redis:6379/0
      - SECRET_KEY=${SECRET_KEY:-o-red-change-this-secret-key-in-production}
      - DEBUG=false
      - LOG_LEVEL=INFO
    depends_on:
      - postgres
      - redis
    networks:
      - ored-network
    volumes:
      - ./logs:/app/logs
      - ./storage:/app/storage
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 30s
      timeout: 10s
      retries: 3

  # Base de données PostgreSQL
  postgres:
    image: postgres:15-alpine
    container_name: ored-postgres
    environment:
      - POSTGRES_DB=ored_central
      - POSTGRES_USER=ored
      - POSTGRES_PASSWORD=ored_secure_password
      - POSTGRES_INITDB_ARGS=--encoding=UTF-8
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./db/init.sql:/docker-entrypoint-initdb.d/init.sql
    networks:
      - ored-network
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ored -d ored_central"]
      interval: 30s
      timeout: 10s
      retries: 3

  # Redis pour cache et sessions
  redis:
    image: redis:7-alpine
    container_name: ored-redis
    command: redis-server --appendonly yes --requirepass ored_redis_password
    volumes:
      - redis_data:/data
    networks:
      - ored-network
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "redis-cli", "--raw", "incr", "ping"]
      interval: 30s
      timeout: 10s
      retries: 3

  # Interface Web O-Red
  ored-web:
    build: 
      context: ../web-interface
      dockerfile: Dockerfile
    container_name: ored-web-interface
    ports:
      - "3000:3000"
    environment:
      - REACT_APP_API_URL=http://localhost:8000
      - REACT_APP_ENVIRONMENT=production
    depends_on:
      - ored-api
    networks:
      - ored-network
    restart: unless-stopped

  # Client nœud P2P (exemple)
  ored-node:
    build:
      context: ../node-client
      dockerfile: Dockerfile
    container_name: ored-node-client
    ports:
      - "8001:8001"
    environment:
      - ORED_API_URL=http://ored-api:8000
      - P2P_PORT=8001
      - NODE_TYPE=full
    depends_on:
      - ored-api
    networks:
      - ored-network
    volumes:
      - node_data:/app/data
      - node_config:/app/config
    restart: unless-stopped

  # Service de monitoring (Prometheus)
  prometheus:
    image: prom/prometheus:latest
    container_name: ored-prometheus
    ports:
      - "9090:9090"
    volumes:
      - ./monitoring/prometheus.yml:/etc/prometheus/prometheus.yml
      - prometheus_data:/prometheus
    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'
    networks:
      - ored-network
    restart: unless-stopped

  # Tableau de bord (Grafana)
  grafana:
    image: grafana/grafana:latest
    container_name: ored-grafana
    ports:
      - "3001:3000"
    environment:
      - GF_SECURITY_ADMIN_PASSWORD=ored_admin_password
      - GF_INSTALL_PLUGINS=grafana-clock-panel,grafana-simple-json-datasource
    volumes:
      - grafana_data:/var/lib/grafana
      - ./monitoring/grafana/provisioning:/etc/grafana/provisioning
    depends_on:
      - prometheus
    networks:
      - ored-network
    restart: unless-stopped

  # Nginx comme proxy inverse
  nginx:
    image: nginx:alpine
    container_name: ored-nginx
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/ssl:/etc/nginx/ssl
      - ./nginx/logs:/var/log/nginx
    depends_on:
      - ored-api
      - ored-web
    networks:
      - ored-network
    restart: unless-stopped

networks:
  ored-network:
    driver: bridge
    name: ored-network

volumes:
  postgres_data:
    name: ored_postgres_data
  redis_data:
    name: ored_redis_data
  node_data:
    name: ored_node_data
  node_config:
    name: ored_node_config
  prometheus_data:
    name: ored_prometheus_data
  grafana_data:
    name: ored_grafana_data