version: '3.8'

# ═══════════════════════════════════════════════════════════
# 🐍 OPHION - Full Observability Stack
# Single docker compose up for complete observability platform
# ═══════════════════════════════════════════════════════════

x-ophion-common: &ophion-common
  restart: unless-stopped
  networks:
    - ophion

services:
  # ─────────────────────────────────────────────────────────
  # 📦 INSTRUMENTATION INIT CONTAINER
  # Downloads all OpenTelemetry agents for all languages
  # ─────────────────────────────────────────────────────────
  instrumentation:
    build:
      context: ./deploy/instrumentation
      dockerfile: Dockerfile
    container_name: ophion-instrumentation
    volumes:
      - otel_agents:/otel
    command: ["echo", "Agents ready in /otel"]
    <<: *ophion-common

  # ─────────────────────────────────────────────────────────
  # 🐍 OPHION Server (Go API)
  # ─────────────────────────────────────────────────────────
  server:
    build:
      context: .
      dockerfile: deploy/docker/Dockerfile.server
    container_name: ophion-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
      - AGENT_KEY=${AGENT_KEY:-ophion-secret-agent-key-2024}
      - JWT_SECRET=${JWT_SECRET:-ophion-jwt-secret-change-in-production}
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "wget", "-q", "--spider", "http://localhost:8080/health"]
      interval: 10s
      timeout: 5s
      retries: 3
    <<: *ophion-common

  # ─────────────────────────────────────────────────────────
  # 🖥️ OPHION Dashboard (Next.js)
  # ─────────────────────────────────────────────────────────
  dashboard:
    build:
      context: ./dashboard
      dockerfile: Dockerfile
    container_name: ophion-dashboard
    ports:
      - "3000:3000"
    environment:
      - NEXT_PUBLIC_API_URL=http://localhost:8080
      - NODE_ENV=production
    depends_on:
      - server
    <<: *ophion-common

  # ─────────────────────────────────────────────────────────
  # 📊 OpenTelemetry Collector
  # Central receiver for all instrumented applications
  # ─────────────────────────────────────────────────────────
  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
      - "4318:4318"   # OTLP HTTP
      - "8889:8889"   # Prometheus metrics
      - "13133:13133" # Health check
    environment:
      - OPHION_SERVER=http://server:8080
      - AGENT_KEY=${AGENT_KEY:-ophion-secret-agent-key-2024}
    depends_on:
      - server
    healthcheck:
      test: ["CMD", "wget", "-q", "--spider", "http://localhost:13133/health"]
      interval: 10s
      timeout: 5s
      retries: 3
    deploy:
      resources:
        limits:
          memory: 512M
    <<: *ophion-common

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

  # ─────────────────────────────────────────────────────────
  # 📈 ClickHouse (Metrics, Traces, Logs - High Volume)
  # ─────────────────────────────────────────────────────────
  clickhouse:
    image: clickhouse/clickhouse-server:24.1
    container_name: ophion-clickhouse
    ports:
      - "9000:9000"   # Native protocol
      - "8123:8123"   # HTTP interface
    volumes:
      - clickhouse_data:/var/lib/clickhouse
    environment:
      - CLICKHOUSE_DB=ophion
    healthcheck:
      test: ["CMD", "clickhouse-client", "--query", "SELECT 1"]
      interval: 5s
      timeout: 5s
      retries: 5
    <<: *ophion-common

  # ─────────────────────────────────────────────────────────
  # 🔴 Redis (Cache, Pub/Sub, Rate Limiting)
  # ─────────────────────────────────────────────────────────
  redis:
    image: redis:7-alpine
    container_name: ophion-redis
    command: redis-server --appendonly yes
    volumes:
      - redis_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 5s
      retries: 5
    <<: *ophion-common

  # ─────────────────────────────────────────────────────────
  # 🤖 OPHION Agent (System Metrics)
  # Collect host metrics and send to server
  # ─────────────────────────────────────────────────────────
  agent:
    build:
      context: .
      dockerfile: deploy/docker/Dockerfile.agent
    container_name: ophion-agent
    environment:
      - OPHION_SERVER=http://server:8080
      - OPHION_API_KEY=${AGENT_KEY:-ophion-secret-agent-key-2024}
      - OPHION_INTERVAL=30s
      - OPHION_DOCKER=true
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
    depends_on:
      server:
        condition: service_healthy
    <<: *ophion-common

networks:
  ophion:
    driver: bridge
    name: ophion-network

volumes:
  postgres_data:
  clickhouse_data:
  redis_data:
  otel_agents:
    name: ophion-otel-agents
