version: '3.8'

services:
  # ====================================
  # SERVEUR CHAT RUST PRINCIPAL
  # ====================================
  chat-server:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8080:8080"
      - "9090:9090"  # Métriques Prometheus
    environment:
      - RUST_ENV=development
      - RUST_LOG=info
      - DATABASE_URL=postgresql://veza_user:veza_password@postgres:5432/veza_chat
      - REDIS_URL=redis://redis:6379
      - JWT_SECRET=super-secret-jwt-key-for-development
      - SERVER_BIND_ADDR=0.0.0.0:8080
      - PROMETHEUS_BIND_ADDR=0.0.0.0:9090
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    volumes:
      - ./logs:/app/logs
      - ./config:/app/config
    restart: unless-stopped
    networks:
      - veza-network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
      interval: 30s
      timeout: 10s
      retries: 3

  # ====================================
  # BASE DE DONNÉES POSTGRESQL
  # ====================================
  postgres:
    image: postgres:15-alpine
    environment:
      POSTGRES_DB: veza_chat
      POSTGRES_USER: veza_user
      POSTGRES_PASSWORD: veza_password
      POSTGRES_INITDB_ARGS: "--auth-host=scram-sha-256"
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./scripts/database/init.sql:/docker-entrypoint-initdb.d/init.sql:ro
    restart: unless-stopped
    networks:
      - veza-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U veza_user -d veza_chat"]
      interval: 10s
      timeout: 5s
      retries: 5

  # ====================================
  # CACHE REDIS
  # ====================================
  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
      - ./config/redis.conf:/usr/local/etc/redis/redis.conf:ro
    command: redis-server /usr/local/etc/redis/redis.conf
    restart: unless-stopped
    networks:
      - veza-network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 3

  # ====================================
  # MONITORING PROMETHEUS (OPTIONNEL)
  # ====================================
  prometheus:
    image: prom/prometheus:latest
    ports:
      - "9091:9090"
    volumes:
      - ./config/prometheus.yml:/etc/prometheus/prometheus.yml:ro
      - 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'
    restart: unless-stopped
    networks:
      - veza-network
    profiles:
      - monitoring

  # ====================================
  # GRAFANA POUR VISUALISATION (OPTIONNEL)
  # ====================================
  grafana:
    image: grafana/grafana:latest
    ports:
      - "3000:3000"
    environment:
      - GF_SECURITY_ADMIN_PASSWORD=admin
      - GF_USERS_ALLOW_SIGN_UP=false
    volumes:
      - grafana_data:/var/lib/grafana
      - ./config/grafana/datasources:/etc/grafana/provisioning/datasources:ro
      - ./config/grafana/dashboards:/etc/grafana/provisioning/dashboards:ro
    restart: unless-stopped
    networks:
      - veza-network
    profiles:
      - monitoring

  # ====================================
  # NGINX REVERSE PROXY (OPTIONNEL)
  # ====================================
  nginx:
    image: nginx:alpine
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./config/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./config/nginx/ssl:/etc/nginx/ssl:ro
      - ./logs/nginx:/var/log/nginx
    depends_on:
      - chat-server
    restart: unless-stopped
    networks:
      - veza-network
    profiles:
      - production

# ====================================
# VOLUMES PERSISTANTS
# ====================================
volumes:
  postgres_data:
    driver: local
  redis_data:
    driver: local
  prometheus_data:
    driver: local
  grafana_data:
    driver: local

# ====================================
# RÉSEAU INTERNE
# ====================================
networks:
  veza-network:
    driver: bridge
    ipam:
      config:
        - subnet: 172.20.0.0/16

# ====================================
# PROFILS DE DÉPLOIEMENT
# ====================================

# Démarrage basique (développement)
# docker-compose up chat-server postgres redis

# Avec monitoring complet
# docker-compose --profile monitoring up

# Production avec proxy
# docker-compose --profile production --profile monitoring up 