# Docker Compose configuration for Veza platform
# version field is obsolete and removed (Docker Compose v2+)

services:
  haproxy:
    image: haproxy:lts-alpine
    container_name: veza-haproxy
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./docker/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
      - ./docker/haproxy/certs:/usr/local/etc/haproxy/certs:ro
    depends_on:
      backend-api:
        condition: service_healthy
      chat-server:
        condition: service_healthy
      stream-server:
        condition: service_healthy
      frontend:
        condition: service_healthy
    networks:
      - veza-network

  postgres:
    image: postgres:15-alpine
    container_name: veza-postgres-local
    environment:
      POSTGRES_DB: veza_db
      POSTGRES_USER: veza
      POSTGRES_PASSWORD: veza_password
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - veza-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U veza -d veza_db"]
      interval: 5s
      timeout: 5s
      retries: 5
      start_period: 10s

  redis:
    image: redis:7-alpine
    container_name: veza-redis-local
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
    networks:
      - veza-network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 3s
      retries: 5
      start_period: 10s

  rabbitmq:
    image: rabbitmq:3.12-management-alpine
    container_name: veza-rabbitmq-local
    ports:
      - "5672:5672"
      - "15672:15672"
    environment:
      RABBITMQ_DEFAULT_USER: veza
      RABBITMQ_DEFAULT_PASS: veza_password
    volumes:
      - rabbitmq_data:/var/lib/rabbitmq
    networks:
      - veza-network
    healthcheck:
      test: ["CMD", "rabbitmq-diagnostics", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 20s

  backend-api:
    build:
      context: ./veza-backend-api
      dockerfile: Dockerfile
    container_name: veza-backend-api-local
    # Ports removed, accessed via HAProxy
    environment:
      DATABASE_URL: postgres://veza:veza_password@postgres:5432/veza_db?sslmode=disable
      REDIS_URL: redis://redis:6379
      RABBITMQ_URL: amqp://veza:veza_password@rabbitmq:5672/
      JWT_SECRET: ${JWT_SECRET:-dev-secret-key-change-in-production}
      APP_ENV: development
      APP_PORT: 8080
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
      rabbitmq:
        condition: service_healthy
    networks:
      - veza-network
    healthcheck:
      test: ["CMD-SHELL", "wget -qO- http://localhost:8080/health || exit 1"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 30s

  chat-server:
    build:
      context: ./veza-chat-server
      dockerfile: Dockerfile
    container_name: veza-chat-server-local
    # Ports removed, accessed via HAProxy
    environment:
      DATABASE_URL: postgres://veza:veza_password@postgres:5432/veza_db?sslmode=disable
      REDIS_URL: redis://redis:6379
      RABBITMQ_URL: amqp://veza:veza_password@rabbitmq:5672/
      CHAT_SERVER_PORT: 8081
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
      rabbitmq:
        condition: service_healthy
    networks:
      - veza-network
    healthcheck:
      test: ["CMD-SHELL", "wget -qO- http://localhost:8081/health || exit 1"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 30s

  stream-server:
    build:
      context: ./veza-stream-server
      dockerfile: Dockerfile
    container_name: veza-stream-server-local
    # Ports removed, accessed via HAProxy
    environment:
      DATABASE_URL: postgres://veza:veza_password@postgres:5432/veza_db?sslmode=disable
      REDIS_URL: redis://redis:6379
      STREAM_PORT: 8082
      RUST_LOG: stream_server=info,tower_http=debug
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - veza-network
    healthcheck:
      test: ["CMD-SHELL", "wget -qO- http://localhost:8082/health || exit 1"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 30s

  frontend:
    build:
      context: ./apps/web
      dockerfile: Dockerfile
      args:
        VITE_API_BASE_URL: /api/v1
        VITE_WS_BASE_URL: /ws
        VITE_STREAM_URL: /
    container_name: veza-frontend-local
    # Ports removed, accessed via HAProxy
    depends_on:
      backend-api:
        condition: service_healthy
      chat-server:
        condition: service_healthy
      stream-server:
        condition: service_healthy
    networks:
      - veza-network
    healthcheck:
      test: ["CMD-SHELL", "wget -q --spider http://localhost:80/health || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 20s

volumes:
  postgres_data:
  redis_data:
  rabbitmq_data:

networks:
  veza-network:
    driver: bridge
