version: '3.8'

services:
  # PostgreSQL Database
  postgres:
    image: postgres:15-alpine
    container_name: clarity_postgres
    environment:
      POSTGRES_DB: clarity_auth
      POSTGRES_USER: clarity_user
      POSTGRES_PASSWORD: secure_password
      POSTGRES_INITDB_ARGS: "--encoding=UTF-8 --lc-collate=C --lc-ctype=C"
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - clarity_network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U clarity_user -d clarity_auth"]
      interval: 30s
      timeout: 10s
      retries: 3

  # MongoDB Database
  mongodb:
    image: mongo:7.0
    container_name: clarity_mongodb
    environment:
      MONGO_INITDB_ROOT_USERNAME: clarity_user
      MONGO_INITDB_ROOT_PASSWORD: secure_password
      MONGO_INITDB_DATABASE: clarity_core
    ports:
      - "27017:27017"
    volumes:
      - mongodb_data:/data/db
      - ./mongo-init:/docker-entrypoint-initdb.d
    networks:
      - clarity_network
    healthcheck:
      test: ["CMD", "mongosh", "--eval", "db.adminCommand('ping')"]
      interval: 30s
      timeout: 10s
      retries: 3

  # Redis Cache
  redis:
    image: redis:7-alpine
    container_name: clarity_redis
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
    networks:
      - clarity_network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 30s
      timeout: 10s
      retries: 3

  # Authentication Service
  auth-service:
    build:
      context: ../../backend/auth-service
      dockerfile: Dockerfile
    container_name: clarity_auth_service
    environment:
      NODE_ENV: development
      PORT: 3001
      DATABASE_URL: postgresql://clarity_user:secure_password@postgres:5432/clarity_auth
      JWT_SECRET: your-super-secret-jwt-key-change-this-in-production
      JWT_EXPIRES_IN: 24h
      REFRESH_TOKEN_EXPIRES_IN: 7d
      LOG_LEVEL: info
    ports:
      - "3001:3001"
    volumes:
      - ../../backend/auth-service:/app
      - /app/node_modules
    depends_on:
      postgres:
        condition: service_healthy
    networks:
      - clarity_network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3001/health"]
      interval: 30s
      timeout: 10s
      retries: 3

  # Core Service (placeholder for future)
  core-service:
    build:
      context: ../../backend/core-service
      dockerfile: Dockerfile
    container_name: clarity_core_service
    environment:
      NODE_ENV: development
      PORT: 3002
      MONGODB_URL: mongodb://clarity_user:secure_password@mongodb:27017/clarity_core?authSource=admin
      REDIS_URL: redis://redis:6379
      AUTH_SERVICE_URL: http://auth-service:3001
    ports:
      - "3002:3002"
    volumes:
      - ../../backend/core-service:/app
      - /app/node_modules
    depends_on:
      mongodb:
        condition: service_healthy
      redis:
        condition: service_healthy
      auth-service:
        condition: service_healthy
    networks:
      - clarity_network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3002/health"]
      interval: 30s
      timeout: 10s
      retries: 3

  # API Gateway (Kong)
  api-gateway:
    image: kong:3.4
    container_name: clarity_api_gateway
    environment:
      KONG_DATABASE: "off"
      KONG_PROXY_ACCESS_LOG: /dev/stdout
      KONG_ADMIN_ACCESS_LOG: /dev/stdout
      KONG_PROXY_ERROR_LOG: /dev/stderr
      KONG_ADMIN_ERROR_LOG: /dev/stderr
      KONG_ADMIN_LISTEN: 0.0.0.0:8001
      KONG_ADMIN_GUI_URL: http://localhost:8002
    ports:
      - "8000:8000"
      - "8001:8001"
      - "8002:8002"
      - "8443:8443"
      - "8444:8444"
    volumes:
      - ./kong.yml:/kong.yml
    networks:
      - clarity_network
    depends_on:
      - auth-service
      - core-service

  # Monitoring (Prometheus)
  prometheus:
    image: prom/prometheus:latest
    container_name: clarity_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'
      - '--web.console.libraries=/etc/prometheus/console_libraries'
      - '--web.console.templates=/etc/prometheus/consoles'
      - '--storage.tsdb.retention.time=200h'
      - '--web.enable-lifecycle'
    networks:
      - clarity_network

  # Monitoring (Grafana)
  grafana:
    image: grafana/grafana:latest
    container_name: clarity_grafana
    ports:
      - "3000:3000"
    environment:
      GF_SECURITY_ADMIN_PASSWORD: admin
    volumes:
      - grafana_data:/var/lib/grafana
      - ./grafana/provisioning:/etc/grafana/provisioning
    networks:
      - clarity_network
    depends_on:
      - prometheus

volumes:
  postgres_data:
  mongodb_data:
  redis_data:
  prometheus_data:
  grafana_data:

networks:
  clarity_network:
    driver: bridge 