version: '3.8'

services:
  # Infrastructure Layer
  redis:
    image: redis:7-alpine
    container_name: sunaivacore-redis
    restart: unless-stopped
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
    command: redis-server --appendonly yes
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 30s
      timeout: 10s
      retries: 3

  postgres:
    image: postgres:15-alpine
    container_name: sunaivacore-postgres
    restart: unless-stopped
    environment:
      POSTGRES_DB: sunaivacore
      POSTGRES_USER: sunaivacore
      POSTGRES_PASSWORD: sunaivacore123
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./infrastructure/init.sql:/docker-entrypoint-initdb.d/init.sql
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U sunaivacore"]
      interval: 30s
      timeout: 10s
      retries: 3

  # MCP Server Layer
  memory-server:
    build:
      context: ./mcp-servers/memory-server
      dockerfile: Dockerfile
    container_name: sunaivacore-memory-server
    restart: unless-stopped
    ports:
      - "3001:3000"
      - "3002:3001"  # WebSocket port
    volumes:
      - memory_data:/app/data
      - ./mcp-servers/memory-server/config.json:/app/config.json:ro
    environment:
      - NODE_ENV=production
      - REDIS_URL=redis://redis:6379
      - DATABASE_URL=postgresql://sunaivacore:sunaivacore123@postgres:5432/sunaivacore
    depends_on:
      - redis
      - postgres
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
      interval: 30s
      timeout: 10s
      retries: 3

  context-server:
    build:
      context: ./mcp-servers/context-server
      dockerfile: Dockerfile
    container_name: sunaivacore-context-server
    restart: unless-stopped
    ports:
      - "3003:3000"
    environment:
      - NODE_ENV=production
      - REDIS_URL=redis://redis:6379
      - MEMORY_SERVER_URL=http://memory-server:3000
    depends_on:
      - redis
      - memory-server

  llm-router-server:
    build:
      context: ./mcp-servers/llm-router-server
      dockerfile: Dockerfile
    container_name: sunaivacore-llm-router
    restart: unless-stopped
    ports:
      - "3004:3000"
    environment:
      - NODE_ENV=production
      - REDIS_URL=redis://redis:6379
      - CONTEXT_SERVER_URL=http://context-server:3000
    depends_on:
      - redis
      - context-server

  compliance-server:
    build:
      context: ./mcp-servers/compliance-server
      dockerfile: Dockerfile
    container_name: sunaivacore-compliance
    restart: unless-stopped
    ports:
      - "3005:3000"
    environment:
      - NODE_ENV=production
      - REDIS_URL=redis://redis:6379
    depends_on:
      - redis

  # Frontend Layer
  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
    container_name: sunaivacore-frontend
    restart: unless-stopped
    ports:
      - "3000:3000"
    environment:
      - NEXT_PUBLIC_API_URL=http://localhost:3001
      - NEXT_PUBLIC_WS_URL=ws://localhost:3002
      - NEXT_PUBLIC_CONTEXT_API=http://localhost:3003
      - NEXT_PUBLIC_LLM_ROUTER=http://localhost:3004
    depends_on:
      - memory-server
      - context-server
      - llm-router-server

  # Monitoring Layer
  nginx:
    image: nginx:alpine
    container_name: sunaivacore-proxy
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./infrastructure/nginx.conf:/etc/nginx/nginx.conf:ro
    depends_on:
      - frontend
      - memory-server

volumes:
  redis_data:
  postgres_data:
  memory_data:

networks:
  default:
    name: sunaivacore-network 