x-defaults: &defaults
  restart: unless-stopped
  logging:
    driver: json-file
    options:
      max-size: ${MULTICA_LOG_MAX_SIZE:-100m}
      max-file: '${MULTICA_LOG_MAX_FILE:-3}'

services:
  multica-postgres:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}pgvector/pgvector:${MULTICA_PGVECTOR_VERSION:-pg17}
    environment:
      - TZ=${TZ:-UTC}
      - POSTGRES_DB=${MULTICA_POSTGRES_DB:-multica}
      - POSTGRES_USER=${MULTICA_POSTGRES_USER:-multica}
      - POSTGRES_PASSWORD=${MULTICA_POSTGRES_PASSWORD:-multica}
    volumes:
      - multica_pgdata:/var/lib/postgresql/data
    healthcheck:
      test: [CMD-SHELL, pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s
    deploy:
      resources:
        limits:
          cpus: ${MULTICA_POSTGRES_CPU_LIMIT:-1.00}
          memory: ${MULTICA_POSTGRES_MEMORY_LIMIT:-1G}
        reservations:
          cpus: ${MULTICA_POSTGRES_CPU_RESERVATION:-0.25}
          memory: ${MULTICA_POSTGRES_MEMORY_RESERVATION:-256M}

  multica-backend:
    <<: *defaults
    build:
      context: https://github.com/multica-ai/multica.git#${MULTICA_VERSION:-v0.1.32}
      dockerfile: Dockerfile
    depends_on:
      multica-postgres:
        condition: service_healthy
    ports:
      - '${MULTICA_BACKEND_PORT_OVERRIDE:-8080}:8080'
    environment:
      - TZ=${TZ:-UTC}
      - DATABASE_URL=postgres://${MULTICA_POSTGRES_USER:-multica}:${MULTICA_POSTGRES_PASSWORD:-multica}@multica-postgres:5432/${MULTICA_POSTGRES_DB:-multica}?sslmode=disable
      - PORT=8080
      - JWT_SECRET=${MULTICA_JWT_SECRET:-change-me-in-production}
      - FRONTEND_ORIGIN=${MULTICA_FRONTEND_ORIGIN:-http://localhost:3000}
      - CORS_ALLOWED_ORIGINS=${MULTICA_CORS_ALLOWED_ORIGINS:-}
      - MULTICA_APP_URL=${MULTICA_APP_URL:-http://localhost:3000}
      - RESEND_API_KEY=${MULTICA_RESEND_API_KEY:-}
      - RESEND_FROM_EMAIL=${MULTICA_RESEND_FROM_EMAIL:-noreply@multica.ai}
      - GOOGLE_CLIENT_ID=${MULTICA_GOOGLE_CLIENT_ID:-}
      - GOOGLE_CLIENT_SECRET=${MULTICA_GOOGLE_CLIENT_SECRET:-}
      - GOOGLE_REDIRECT_URI=${MULTICA_GOOGLE_REDIRECT_URI:-http://localhost:3000/auth/callback}
      - COOKIE_DOMAIN=${MULTICA_COOKIE_DOMAIN:-}
    healthcheck:
      test:
        - CMD-SHELL
        - wget --no-verbose --tries=1 --spider http://127.0.0.1:8080/ || exit 1
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 60s
    deploy:
      resources:
        limits:
          cpus: ${MULTICA_BACKEND_CPU_LIMIT:-2.00}
          memory: ${MULTICA_BACKEND_MEMORY_LIMIT:-2G}
        reservations:
          cpus: ${MULTICA_BACKEND_CPU_RESERVATION:-0.50}
          memory: ${MULTICA_BACKEND_MEMORY_RESERVATION:-512M}

  multica-frontend:
    <<: *defaults
    build:
      context: https://github.com/multica-ai/multica.git#${MULTICA_VERSION:-v0.1.32}
      dockerfile: Dockerfile.web
      args:
        REMOTE_API_URL: http://multica-backend:8080
        NEXT_PUBLIC_GOOGLE_CLIENT_ID: ${MULTICA_GOOGLE_CLIENT_ID:-}
    depends_on:
      - multica-backend
    ports:
      - '${MULTICA_FRONTEND_PORT_OVERRIDE:-3000}:3000'
    environment:
      - TZ=${TZ:-UTC}
      - HOSTNAME=0.0.0.0
    healthcheck:
      test:
        - CMD-SHELL
        - wget --no-verbose --tries=1 --spider http://127.0.0.1:3000/ || exit 1
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 60s
    deploy:
      resources:
        limits:
          cpus: ${MULTICA_FRONTEND_CPU_LIMIT:-1.00}
          memory: ${MULTICA_FRONTEND_MEMORY_LIMIT:-1G}
        reservations:
          cpus: ${MULTICA_FRONTEND_CPU_RESERVATION:-0.25}
          memory: ${MULTICA_FRONTEND_MEMORY_RESERVATION:-256M}

volumes:
  multica_pgdata:
