# docker-compose.yml
# client2server with Caddy load balancer

services:
  # ========================================
  # Redpanda (Kafka-compatible event store)
  # ========================================
  redpanda:
    image: docker.redpanda.com/vectorized/redpanda:v23.11.10
    container_name: client2server-redpanda
    hostname: redpanda
    command: redpanda start --overprovisioned --smp 1 --memory 1G --node-id 0 --pandaproxy-port=8082 --schema-registry-port=8081
    ports:
      - "8082:8082"  # REST API
      - "8081:8081"  # Schema registry
      - "9092:9092"  # Kafka
    environment:
      - REDPANDA_AUTO_CREATE_TOPICS=true
    volumes:
      - redpanda_data:/var/lib/redpanda/data
    networks:
      - client2server
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8082/health"]
      interval: 10s
      timeout: 5s
      retries: 5

  # ========================================
  # Go Server Instance 1
  # ========================================
  server1:
    build:
      context: ./server
      dockerfile: Dockerfile
    container_name: client2server-server1
    environment:
      - REDPANDA_BROKERS=redpanda:9092
      - TOKEN=${TOKEN:-secret-token}
      - JWT_SECRET=${JWT_SECRET:-dev-insecure-jwt-secret-change-me-please-32b}
      - PORT=3843
      - DB_PATH=/var/lib/c2s/client2server.db
    volumes:
      - c2s_data:/var/lib/c2s
    depends_on:
      redpanda:
        condition: service_healthy
    networks:
      - client2server
    restart: unless-stopped

  # ========================================
  # Go Server Instance 2
  # ========================================
  server2:
    build:
      context: ./server
      dockerfile: Dockerfile
    container_name: client2server-server2
    environment:
      - REDPANDA_BROKERS=redpanda:9092
      - TOKEN=${TOKEN:-secret-token}
      - JWT_SECRET=${JWT_SECRET:-dev-insecure-jwt-secret-change-me-please-32b}
      - PORT=3843
      - DB_PATH=/var/lib/c2s/client2server.db
    volumes:
      - c2s_data:/var/lib/c2s
    depends_on:
      redpanda:
        condition: service_healthy
    networks:
      - client2server
    restart: unless-stopped

  # ========================================
  # Go Server Instance 3 (optional)
  # ========================================
  # server3:
  #   build:
  #     context: ./server
  #     dockerfile: Dockerfile
  #   container_name: client2server-server3
  #   environment:
  #     - REDPANDA_BROKERS=redpanda:9092
  #     - TOKEN=${TOKEN:-secret-token}
  #     - PORT=3843
  #   depends_on:
  #     redpanda:
  #       condition: service_healthy
  #   networks:
  #     - client2server
  #   restart: unless-stopped

  # ========================================
  # Caddy Load Balancer
  # ========================================
  caddy:
    image: caddy:2-alpine
    container_name: client2server-caddy
    ports:
      - "80:80"
      - "443:443"
      - "3843:3843"  # Unified WebSocket + HTTP API
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile:ro
      - caddy_data:/data
      - caddy_config:/config
    environment:
      - CADDY_INGRESS_NETWORK=caddy
    depends_on:
      - server1
      - server2
      - dashboard
    networks:
      - client2server
    restart: unless-stopped

  # ========================================
  # Dashboard (React SPA served by its own Caddy)
  # ========================================
  dashboard:
    build:
      context: ./dashboard
      dockerfile: Dockerfile
    container_name: client2server-dashboard
    networks:
      - client2server
    restart: unless-stopped

networks:
  client2server:
    driver: bridge

volumes:
  redpanda_data:
  caddy_data:
  caddy_config:
  c2s_data: