version: '3.8'

services:
  # PostgreSQL Database for KMS
  kms-postgres:
    image: docker.io/library/postgres:15-alpine
    container_name: kms-postgres
    environment:
      POSTGRES_DB: kms
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
    ports:
      - "5432:5432"
    volumes:
      - kms_postgres_data:/var/lib/postgresql/data
      - ./kms/migrations:/docker-entrypoint-initdb.d:Z
    networks:
      - skybridge-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres -d kms"]
      interval: 10s
      timeout: 5s
      retries: 5

  # PostgreSQL Database for FaaS
  faas-postgres:
    image: docker.io/library/postgres:15-alpine
    container_name: faas-postgres
    environment:
      POSTGRES_DB: faas
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
    ports:
      - "5433:5432"
    volumes:
      - faas_postgres_data:/var/lib/postgresql/data
      - ./faas/migrations:/docker-entrypoint-initdb.d:Z
    networks:
      - skybridge-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres -d faas"]
      interval: 10s
      timeout: 5s
      retries: 5

  # KMS API Service
  kms-api-service:
    build:
      context: ./kms
      dockerfile: Dockerfile
    container_name: kms-api-service
    environment:
      APP_ENV: development
      DB_HOST: kms-postgres
      DB_PORT: 5432
      DB_NAME: kms
      DB_USER: postgres
      DB_PASSWORD: postgres
      DB_SSLMODE: disable
      DB_CONN_MAX_LIFETIME: 5m
      DB_MAX_OPEN_CONNS: 25
      DB_MAX_IDLE_CONNS: 5
      SERVER_HOST: 0.0.0.0
      SERVER_PORT: 8080
      LOG_LEVEL: debug
      MIGRATION_PATH: /app/migrations
      INTERNAL_HMAC_KEY: 3924f352b7ea63b27db02bf4b0014f2961a5d2f7c27643853a4581bb3a5457cb
      JWT_SECRET: 7f5e11d55e957988b00ce002418680af384219ef98c50d08cbbbdd541978450c
      AUTH_SIGNING_KEY: 484f921b39c383e6b3e0cc5a7cef3c2cec3d7c8d474ab5102891dc4c2bf63a68
      AUTH_PROVIDER: header
      AUTH_HEADER_USER_EMAIL: X-User-Email
      RATE_LIMIT_ENABLED: true
      CACHE_ENABLED: false
      METRICS_ENABLED: true
    ports:
      - "8080:8080"
      - "9090:9090" # Metrics port
    depends_on:
      kms-postgres:
        condition: service_healthy
    networks:
      - skybridge-network
    volumes:
      - ./kms/migrations:/app/migrations:ro,Z
    restart: unless-stopped

  # FaaS API Service
  faas-api-service:
    build:
      context: ./faas
      dockerfile: Dockerfile
    container_name: faas-api-service
    environment:
      FAAS_APP_ENV: development
      FAAS_DB_HOST: faas-postgres
      FAAS_DB_PORT: 5432
      FAAS_DB_NAME: faas
      FAAS_DB_USER: postgres
      FAAS_DB_PASSWORD: postgres
      FAAS_DB_SSLMODE: disable
      DB_CONN_MAX_LIFETIME: 5m
      DB_MAX_OPEN_CONNS: 25
      DB_MAX_IDLE_CONNS: 5
      FAAS_SERVER_HOST: 0.0.0.0
      FAAS_SERVER_PORT: 8082
      FAAS_LOG_LEVEL: debug
      FAAS_DEFAULT_RUNTIME: docker
      FAAS_FUNCTION_TIMEOUT: 300s
      FAAS_MAX_MEMORY: 3008
      FAAS_MAX_CONCURRENT: 100
      FAAS_SANDBOX_ENABLED: true
      FAAS_NETWORK_ISOLATION: true
      FAAS_RESOURCE_LIMITS: true
      AUTH_PROVIDER: header
      AUTH_HEADER_USER_EMAIL: X-User-Email
      RATE_LIMIT_ENABLED: true
      METRICS_ENABLED: true
      METRICS_PORT: 9091
    ports:
      - "8082:8082"
      - "9091:9091" # Metrics port
    depends_on:
      faas-postgres:
        condition: service_healthy
    networks:
      - skybridge-network
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro # For Docker runtime
      - ./faas/migrations:/app/migrations:ro,Z
    restart: unless-stopped

  # Shell Dashboard
  shell-dashboard:
    build:
      context: ./web
      dockerfile: Dockerfile
    container_name: shell-dashboard
    ports:
      - "3000:80"
    networks:
      - skybridge-network
    restart: unless-stopped

  # Demo Module
  demo-module:
    build:
      context: ./demo
      dockerfile: Dockerfile
    container_name: demo-module
    ports:
      - "3001:80"
    networks:
      - skybridge-network
    restart: unless-stopped

  # KMS Frontend
  kms-frontend:
    build:
      context: ./kms/web
      dockerfile: Dockerfile
    container_name: kms-frontend
    ports:
      - "3002:80"
    networks:
      - skybridge-network
    restart: unless-stopped

  # FaaS Frontend
  faas-frontend:
    build:
      context: ./faas/web
      dockerfile: Dockerfile
    container_name: faas-frontend
    ports:
      - "3003:80"
    networks:
      - skybridge-network
    restart: unless-stopped

  # Nginx Reverse Proxy
  nginx:
    image: docker.io/library/nginx:alpine
    container_name: skybridge-nginx
    ports:
      - "8081:80"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro,Z
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf:ro,Z
    depends_on:
      - kms-api-service
      - faas-api-service
      - shell-dashboard
      - demo-module
      - kms-frontend
      - faas-frontend
    networks:
      - skybridge-network
    restart: unless-stopped

volumes:
  kms_postgres_data:
    driver: local
  faas_postgres_data:
    driver: local

networks:
  skybridge-network:
    driver: bridge