version: '3.9'

services:
  # NATS JetStream - Agent broker
  nats:
    image: nats:2.10-alpine
    container_name: lifecycle-nats
    ports:
      - "4222:4222"      # NATS client
      - "8222:8222"      # HTTP monitoring
    command:
      - "-js"            # Enable JetStream
      - "-sd"            # Persistent storage
      - "/data"
    volumes:
      - nats-data:/data
    healthcheck:
      test: ["CMD", "nc", "-z", "localhost", "4222"]
      interval: 10s
      timeout: 3s
      retries: 5
    networks:
      - lifecycle-network
    restart: unless-stopped

  # SQLite Database (mounted volume)
  # SQLite runs embedded in API, but data is shared

  # Lifecycle API Server
  api:
    build:
      context: .
      dockerfile: Dockerfile
      target: api-runtime
    container_name: lifecycle-api
    ports:
      - "3000:3000"
    environment:
      - DATABASE_URL=sqlite:///./projects.db
      - RUST_LOG=info,lifecycle_api=debug
      - NATS_URL=nats://nats:4222
    volumes:
      - ./data:/app/data
    depends_on:
      nats:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
      interval: 10s
      timeout: 3s
      retries: 5
    networks:
      - lifecycle-network
    restart: unless-stopped

  # Web Dashboard Server
  dashboard:
    build:
      context: .
      dockerfile: Dockerfile
      target: dashboard-runtime
    container_name: lifecycle-dashboard
    ports:
      - "3001:3001"
    environment:
      - API_URL=http://api:3000
      - RUST_LOG=info,lifecycle_dashboard=debug
    depends_on:
      api:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3001/"]
      interval: 10s
      timeout: 3s
      retries: 5
    networks:
      - lifecycle-network
    restart: unless-stopped

  # Doc-Lifecycle-Manager (external service)
  doc-lifecycle:
    image: doc-lifecycle:latest
    container_name: lifecycle-docs
    ports:
      - "7000:7000"
    environment:
      - RUST_LOG=info
      - DATABASE_URL=sqlite:///./docs.db
    volumes:
      - ./docs-data:/app/data
    networks:
      - lifecycle-network
    restart: unless-stopped
    profiles:
      - with-docs

  # Prometheus for metrics (optional)
  prometheus:
    image: prom/prometheus:latest
    container_name: lifecycle-prometheus
    ports:
      - "9090:9090"
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
      - prometheus-data:/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
    networks:
      - lifecycle-network
    restart: unless-stopped
    profiles:
      - monitoring

  # Grafana for visualization (optional)
  grafana:
    image: grafana/grafana:latest
    container_name: lifecycle-grafana
    ports:
      - "3002:3000"
    environment:
      - GF_SECURITY_ADMIN_PASSWORD=admin
      - GF_USERS_ALLOW_SIGN_UP=false
    volumes:
      - grafana-data:/var/lib/grafana
    depends_on:
      - prometheus
    networks:
      - lifecycle-network
    restart: unless-stopped
    profiles:
      - monitoring

volumes:
  nats-data:
    driver: local
  prometheus-data:
    driver: local
  grafana-data:
    driver: local

networks:
  lifecycle-network:
    driver: bridge
