# AI Data Aggregation — Automated Ergonode Product Enrichment (PoC)
# One `docker compose up --build` brings up: Postgres, the Symfony backend
# (FrankenPHP), a queue worker, a poller, and the Next.js Admin frontend.
#
# Copy .env.example to .env and fill in the secrets (especially the Vertex AI
# block) before going live. The stack runs fully on mocks out of the box.

services:
  db:
    image: postgres:16-alpine
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-app}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-app}
      POSTGRES_DB: ${POSTGRES_DB:-app}
    volumes:
      - db_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-app}"]
      interval: 5s
      timeout: 5s
      retries: 10
    ports:
      - "5432:5432"

  backend:
    build: ./backend
    depends_on:
      db:
        condition: service_healthy
    environment:
      APP_ENV: prod
      APP_DEBUG: "0"
      APP_SECRET: ${APP_SECRET:-change_me_in_secrets_store}
      DATABASE_URL: "postgresql://${POSTGRES_USER:-app}:${POSTGRES_PASSWORD:-app}@db:5432/${POSTGRES_DB:-app}?serverVersion=16&charset=utf8"
      MESSENGER_TRANSPORT_DSN: "doctrine://default?auto_setup=1"
      ERGONODE_BASE_URL: ${ERGONODE_BASE_URL:-https://digtag-staging.ergonode.cloud}
      ERGONODE_USERNAME: ${ERGONODE_USERNAME:-}
      ERGONODE_PASSWORD: ${ERGONODE_PASSWORD:-}
      ERGONODE_LOG_ATTRIBUTE: ${ERGONODE_LOG_ATTRIBUTE:-ai_product_log}
      ERGONODE_LANGUAGE: ${ERGONODE_LANGUAGE:-en_GB}
      VERTEX_PROJECT_ID: ${VERTEX_PROJECT_ID:-}
      VERTEX_LOCATION: ${VERTEX_LOCATION:-europe-west6}
      VERTEX_CREDENTIALS_FILE: var/secrets/vertex-sa.json
      VERTEX_MODEL_RESEARCH: ${VERTEX_MODEL_RESEARCH:-gemini-2.5-pro}
      VERTEX_MODEL_MANIPULATION: ${VERTEX_MODEL_MANIPULATION:-gemini-2.5-flash}
      VERTEX_MODEL_SCORING: ${VERTEX_MODEL_SCORING:-gemini-2.5-flash}
      VERTEX_MODEL_ALT_TEXT: ${VERTEX_MODEL_ALT_TEXT:-gemini-2.5-flash}
      VERTEX_MODEL_IMAGE: ${VERTEX_MODEL_IMAGE:-gemini-2.5-flash-image}
      VERTEX_MODEL_SEGMENTATION: ${VERTEX_MODEL_SEGMENTATION:-image-segmentation-001}
      SLACK_WEBHOOK_URL: ${SLACK_WEBHOOK_URL:-}
      SLACK_IT_CHANNEL: ${SLACK_IT_CHANNEL:-#it-alerts}
      ADMIN_API_TOKEN: ${ADMIN_API_TOKEN:-dev-admin-token-change-me}
      CORS_ALLOW_ORIGIN: ${CORS_ALLOW_ORIGIN:-^https?://(localhost|127\.0\.0\.1)(:[0-9]+)?$$}
    volumes:
      - ./secrets:/app/var/secrets
      - backend_images:/app/var/images
      - backend_mock:/app/var/mock-ergonode
    healthcheck:
      test: ["CMD-SHELL", "php -r \"exit(@file_get_contents('http://127.0.0.1:8000/api/health')?0:1);\""]
      interval: 10s
      timeout: 5s
      retries: 12
    ports:
      - "8000:8000"

  worker:
    build: ./backend
    depends_on:
      backend:
        condition: service_healthy
    environment:
      APP_ENV: prod
      APP_SECRET: ${APP_SECRET:-change_me_in_secrets_store}
      RUN_MIGRATIONS: "0"
      DATABASE_URL: "postgresql://${POSTGRES_USER:-app}:${POSTGRES_PASSWORD:-app}@db:5432/${POSTGRES_DB:-app}?serverVersion=16&charset=utf8"
      MESSENGER_TRANSPORT_DSN: "doctrine://default?auto_setup=1"
      ERGONODE_BASE_URL: ${ERGONODE_BASE_URL:-https://digtag-staging.ergonode.cloud}
      ERGONODE_USERNAME: ${ERGONODE_USERNAME:-}
      ERGONODE_PASSWORD: ${ERGONODE_PASSWORD:-}
      ERGONODE_LOG_ATTRIBUTE: ${ERGONODE_LOG_ATTRIBUTE:-ai_product_log}
      ERGONODE_LANGUAGE: ${ERGONODE_LANGUAGE:-en_GB}
      VERTEX_PROJECT_ID: ${VERTEX_PROJECT_ID:-}
      VERTEX_LOCATION: ${VERTEX_LOCATION:-europe-west6}
      VERTEX_CREDENTIALS_FILE: var/secrets/vertex-sa.json
      VERTEX_MODEL_RESEARCH: ${VERTEX_MODEL_RESEARCH:-gemini-2.5-pro}
      VERTEX_MODEL_MANIPULATION: ${VERTEX_MODEL_MANIPULATION:-gemini-2.5-flash}
      VERTEX_MODEL_SCORING: ${VERTEX_MODEL_SCORING:-gemini-2.5-flash}
      VERTEX_MODEL_ALT_TEXT: ${VERTEX_MODEL_ALT_TEXT:-gemini-2.5-flash}
      VERTEX_MODEL_IMAGE: ${VERTEX_MODEL_IMAGE:-gemini-2.5-flash-image}
      VERTEX_MODEL_SEGMENTATION: ${VERTEX_MODEL_SEGMENTATION:-image-segmentation-001}
      SLACK_WEBHOOK_URL: ${SLACK_WEBHOOK_URL:-}
      SLACK_IT_CHANNEL: ${SLACK_IT_CHANNEL:-#it-alerts}
    volumes:
      - ./secrets:/app/var/secrets:ro
      - backend_images:/app/var/images
      - backend_mock:/app/var/mock-ergonode
    command: ["php", "bin/console", "app:worker"]

  poller:
    build: ./backend
    depends_on:
      backend:
        condition: service_healthy
    environment:
      APP_ENV: prod
      APP_SECRET: ${APP_SECRET:-change_me_in_secrets_store}
      RUN_MIGRATIONS: "0"
      DATABASE_URL: "postgresql://${POSTGRES_USER:-app}:${POSTGRES_PASSWORD:-app}@db:5432/${POSTGRES_DB:-app}?serverVersion=16&charset=utf8"
      MESSENGER_TRANSPORT_DSN: "doctrine://default?auto_setup=1"
      ERGONODE_BASE_URL: ${ERGONODE_BASE_URL:-https://digtag-staging.ergonode.cloud}
      ERGONODE_USERNAME: ${ERGONODE_USERNAME:-}
      ERGONODE_PASSWORD: ${ERGONODE_PASSWORD:-}
      ERGONODE_LOG_ATTRIBUTE: ${ERGONODE_LOG_ATTRIBUTE:-ai_product_log}
      ERGONODE_LANGUAGE: ${ERGONODE_LANGUAGE:-en_GB}
    volumes:
      - ./secrets:/app/var/secrets:ro
      - backend_mock:/app/var/mock-ergonode
    # Poll on a short loop; ingestion still respects the configurable interval,
    # the ingestion feature flag and the kill-switch. Use a cron in production.
    command: ["sh", "-c", "while true; do php bin/console app:poll || true; sleep 60; done"]

  frontend:
    build: ./frontend
    depends_on:
      - backend
    environment:
      BACKEND_INTERNAL_URL: http://backend:8000
      ADMIN_API_TOKEN: ${ADMIN_API_TOKEN:-dev-admin-token-change-me}
      AUTH_SECRET: ${AUTH_SECRET:-dev-insecure-secret-change-me}
      ALLOW_DEV_LOGIN: ${ALLOW_DEV_LOGIN:-true}
      GOOGLE_CLIENT_ID: ${GOOGLE_CLIENT_ID:-}
      GOOGLE_CLIENT_SECRET: ${GOOGLE_CLIENT_SECRET:-}
      ADMIN_ALLOWED_EMAILS: ${ADMIN_ALLOWED_EMAILS:-}
    ports:
      - "3000:3000"

volumes:
  db_data:
  backend_images:
  backend_mock:
