version: '3.8'

# ═══════════════════════════════════════════════════════════
# 🐍 OPHION - Docker Compose
# Observability Platform with ClickHouse, PostgreSQL, Redis
# ═══════════════════════════════════════════════════════════

services:
  # ─────────────────────────────────────────────────────────
  # OPHION Server (Go API)
  # ─────────────────────────────────────────────────────────
  server:
    build:
      context: .
      dockerfile: deploy/docker/Dockerfile.server
    ports:
      - "8080:8080"
    environment:
      - PORT=8080
      - DATABASE_URL=postgres://ophion:ophion@postgres:5432/ophion?sslmode=disable
      - CLICKHOUSE_URL=clickhouse://default:@clickhouse:9000/ophion
      - REDIS_URL=redis://redis:6379
    depends_on:
      postgres:
        condition: service_healthy
      clickhouse:
        condition: service_healthy
      redis:
        condition: service_healthy
    restart: unless-stopped
    networks:
      - ophion
    healthcheck:
      test: ["CMD", "wget", "-q", "--spider", "http://localhost:8080/health"]
      interval: 10s
      timeout: 5s
      retries: 3

  # ─────────────────────────────────────────────────────────
  # OPHION Dashboard (Next.js)
  # ─────────────────────────────────────────────────────────
  dashboard:
    build:
      context: ./dashboard
      dockerfile: Dockerfile
    ports:
      - "3000:3000"
    environment:
      - NEXT_PUBLIC_API_URL=http://server:8080
      - NODE_ENV=production
    depends_on:
      - server
    restart: unless-stopped
    networks:
      - ophion

  # ─────────────────────────────────────────────────────────
  # PostgreSQL (Metadata, Users, Alerts)
  # ─────────────────────────────────────────────────────────
  postgres:
    image: postgres:16-alpine
    environment:
      POSTGRES_USER: ophion
      POSTGRES_PASSWORD: ophion
      POSTGRES_DB: ophion
    volumes:
      - postgres_data:/var/lib/postgresql/data
    restart: unless-stopped
    networks:
      - ophion
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ophion"]
      interval: 5s
      timeout: 5s
      retries: 5

  # ─────────────────────────────────────────────────────────
  # ClickHouse (Metrics, Traces, Logs)
  # ─────────────────────────────────────────────────────────
  clickhouse:
    image: clickhouse/clickhouse-server:24.1
    ports:
      - "9000:9000"   # Native protocol
      - "8123:8123"   # HTTP interface
    volumes:
      - clickhouse_data:/var/lib/clickhouse
      - ./configs/clickhouse:/etc/clickhouse-server/config.d
    environment:
      - CLICKHOUSE_DB=ophion
    restart: unless-stopped
    networks:
      - ophion
    healthcheck:
      test: ["CMD", "clickhouse-client", "--query", "SELECT 1"]
      interval: 5s
      timeout: 5s
      retries: 5

  # ─────────────────────────────────────────────────────────
  # Redis (Cache, Pub/Sub)
  # ─────────────────────────────────────────────────────────
  redis:
    image: redis:7-alpine
    command: redis-server --appendonly yes
    volumes:
      - redis_data:/data
    restart: unless-stopped
    networks:
      - ophion
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 5s
      retries: 5

  # ─────────────────────────────────────────────────────────
  # OpenTelemetry Collector (Traces, Metrics, Logs)
  # ─────────────────────────────────────────────────────────
  otel-collector:
    image: otel/opentelemetry-collector-contrib:0.96.0
    container_name: ophion-otel-collector
    command: ["--config=/etc/otel-collector-config.yaml"]
    volumes:
      - ./deploy/docker/otel-collector-config.yaml:/etc/otel-collector-config.yaml:ro
    ports:
      - "4317:4317"   # OTLP gRPC receiver
      - "4318:4318"   # OTLP HTTP receiver
      - "8889:8889"   # Prometheus exporter metrics
      - "13133:13133" # Health check extension
    environment:
      - OTEL_RESOURCE_ATTRIBUTES=service.name=ophion-collector,service.version=1.0.0
    depends_on:
      - server
    restart: unless-stopped
    networks:
      - ophion
    healthcheck:
      test: ["CMD", "wget", "-q", "--spider", "http://localhost:13133/health"]
      interval: 10s
      timeout: 5s
      retries: 3
    deploy:
      resources:
        limits:
          memory: 512M
        reservations:
          memory: 128M

networks:
  ophion:
    driver: bridge

volumes:
  postgres_data:
  clickhouse_data:
  redis_data:
