# ─── Arcádia Suite — Desenvolvimento Local ────────────────────────────────────
# Uso: docker compose up
# Para subir com IA local: docker compose --profile ai up

name: arcadia-dev

services:

  # ── Banco de dados com pgvector ─────────────────────────────────────────────
  db:
    image: pgvector/pgvector:pg16
    restart: unless-stopped
    environment:
      POSTGRES_DB: arcadia
      POSTGRES_USER: arcadia
      POSTGRES_PASSWORD: arcadia123
    volumes:
      - pgdata:/var/lib/postgresql/data
      - ./docker/init-pgvector.sql:/docker-entrypoint-initdb.d/01-pgvector.sql
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U arcadia -d arcadia"]
      interval: 10s
      timeout: 5s
      retries: 5

  # ── Redis (filas de jobs) ────────────────────────────────────────────────────
  redis:
    image: redis:7-alpine
    restart: unless-stopped
    ports:
      - "6379:6379"
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

  # ── App principal (Node.js + React) ─────────────────────────────────────────
  app:
    build:
      context: .
      dockerfile: Dockerfile
      target: builder          # usa stage builder em dev (hot reload via volume)
    restart: unless-stopped
    command: npx tsx server/index.ts
    environment:
      NODE_ENV: development
      PORT: 5000
      DATABASE_URL: postgresql://arcadia:arcadia123@db:5432/arcadia
      REDIS_URL: redis://redis:6379
      DOCKER_MODE: "true"      # desativa spawn de processos filhos
      CONTABIL_PYTHON_URL: http://contabil:8003
      BI_PYTHON_URL: http://bi:8004
      AUTOMATION_PYTHON_URL: http://automation:8005
      FISCO_PYTHON_URL: http://fisco:8002
      PYTHON_SERVICE_URL: http://embeddings:8001
      SESSION_SECRET: ${SESSION_SECRET:-arcadia-dev-secret-change-in-prod}
      SSO_SECRET: ${SSO_SECRET:-arcadia-sso-secret-2024-plus-integration-key-secure}
      OPENAI_API_KEY: ${OPENAI_API_KEY:-}
      LITELLM_BASE_URL: http://litellm:4000
      LITELLM_API_KEY: ${LITELLM_API_KEY:-arcadia-internal}
    ports:
      - "5000:5000"
    volumes:
      - .:/app
      - /app/node_modules
      - /app/dist
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
      contabil:
        condition: service_started
      bi:
        condition: service_started

  # ── Microserviço Contábil (Python) ──────────────────────────────────────────
  contabil:
    build:
      context: .
      dockerfile: Dockerfile.python
    restart: unless-stopped
    environment:
      SERVICE_NAME: contabil
      SERVICE_PORT: 8003
      CONTABIL_PORT: 8003
      DATABASE_URL: postgresql://arcadia:arcadia123@db:5432/arcadia
    ports:
      - "8003:8003"
    depends_on:
      db:
        condition: service_healthy

  # ── Microserviço BI Engine (Python) ─────────────────────────────────────────
  bi:
    build:
      context: .
      dockerfile: Dockerfile.python
    restart: unless-stopped
    environment:
      SERVICE_NAME: bi
      SERVICE_PORT: 8004
      BI_PORT: 8004
      DATABASE_URL: postgresql://arcadia:arcadia123@db:5432/arcadia
    ports:
      - "8004:8004"
    depends_on:
      db:
        condition: service_healthy

  # ── Microserviço Automações (Python) ────────────────────────────────────────
  automation:
    build:
      context: .
      dockerfile: Dockerfile.python
    restart: unless-stopped
    environment:
      SERVICE_NAME: automation
      SERVICE_PORT: 8005
      AUTOMATION_PORT: 8005
      DATABASE_URL: postgresql://arcadia:arcadia123@db:5432/arcadia
    ports:
      - "8005:8005"
    depends_on:
      db:
        condition: service_healthy

  # ── Microserviço Fiscal (Python) ────────────────────────────────────────────
  fisco:
    build:
      context: .
      dockerfile: Dockerfile.python
    restart: unless-stopped
    environment:
      SERVICE_NAME: fisco
      SERVICE_PORT: 8002
      FISCO_PORT: 8002
      DATABASE_URL: postgresql://arcadia:arcadia123@db:5432/arcadia
    ports:
      - "8002:8002"
    depends_on:
      db:
        condition: service_healthy

  # ── Serviço de Embeddings (pgvector via Python) ──────────────────────────────
  embeddings:
    build:
      context: .
      dockerfile: Dockerfile.python
    restart: unless-stopped
    environment:
      SERVICE_NAME: embeddings
      SERVICE_PORT: 8001
      DATABASE_URL: postgresql://arcadia:arcadia123@db:5432/arcadia
    ports:
      - "8001:8001"
    depends_on:
      db:
        condition: service_healthy

  # ── Apache Superset (BI avançado) ────────────────────────────────────────────
  superset:
    image: apache/superset:4.1.0
    restart: unless-stopped
    environment:
      SUPERSET_SECRET_KEY: ${SUPERSET_SECRET_KEY:-superset-secret-change-in-prod}
      DATABASE_URL: postgresql://${PGUSER:-arcadia}:${PGPASSWORD:-arcadia123}@db:5432/arcadia_superset
      ARCADIA_DATABASE_URL: postgresql://${PGUSER:-arcadia}:${PGPASSWORD:-arcadia123}@db:5432/arcadia
      SUPERSET_ADMIN_USER: ${SUPERSET_ADMIN_USER:-admin}
      SUPERSET_ADMIN_EMAIL: ${SUPERSET_ADMIN_EMAIL:-admin@arcadia.app}
      SUPERSET_ADMIN_PASSWORD: ${SUPERSET_ADMIN_PASSWORD:-arcadia2026}
      SUPERSET_WEBSERVER_PORT: 8088
      PYTHONPATH: /app/pythonpath
      FLASK_ENV: production
    ports:
      - "8088:8088"
    volumes:
      - ./docker/superset/superset_config.py:/app/pythonpath/superset_config.py:ro
      - ./docker/superset/init.sh:/app/docker/init.sh:ro
      - superset_home:/app/superset_home
    depends_on:
      db:
        condition: service_healthy
    command: ["/bin/bash", "/app/docker/init.sh"]
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8088/health"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 60s
    networks:
      - arcadia
    profiles: [bi]

  # ─────────────── PERFIL: ai (Soberania de IA) ────────────────────────────────

  # ── Ollama (LLMs locais) ─────────────────────────────────────────────────────
  ollama:
    image: ollama/ollama:latest
    restart: unless-stopped
    volumes:
      - ollama_models:/root/.ollama
    ports:
      - "11434:11434"
    profiles: [ai]
    # Para GPU NVIDIA: adicione `deploy.resources.reservations.devices`
    # deploy:
    #   resources:
    #     reservations:
    #       devices:
    #         - driver: nvidia
    #           count: 1
    #           capabilities: [gpu]

  # ── Open WebUI (interface para devs/consultores) ─────────────────────────────
  open-webui:
    image: ghcr.io/open-webui/open-webui:main
    restart: unless-stopped
    environment:
      OLLAMA_BASE_URL: http://ollama:11434
      WEBUI_SECRET_KEY: ${WEBUI_SECRET_KEY:-webui-secret-change-in-prod}
      DEFAULT_MODELS: llama3.3,qwen2.5-coder
      ENABLE_RAG_WEB_SEARCH: "true"
    ports:
      - "3001:8080"
    volumes:
      - open_webui_data:/app/backend/data
    depends_on:
      - ollama
    profiles: [ai]

  # ── LiteLLM (proxy unificado de LLMs) ───────────────────────────────────────
  litellm:
    image: ghcr.io/berriai/litellm:main-latest
    restart: unless-stopped
    volumes:
      - ./docker/litellm-config.yaml:/app/config.yaml
    command: ["--config", "/app/config.yaml", "--port", "4000", "--detailed_debug"]
    environment:
      OPENAI_API_KEY: ${OPENAI_API_KEY:-}
      OLLAMA_BASE_URL: http://ollama:11434
      LITELLM_MASTER_KEY: ${LITELLM_API_KEY:-arcadia-internal}
    ports:
      - "4000:4000"
    profiles: [ai]

  # ─────────────── PERFIL: erpnext (ERPNext local) ──────────────────────────────

  # ── MariaDB para ERPNext ─────────────────────────────────────────────────────
  erpnext-db:
    image: mariadb:10.6
    restart: unless-stopped
    profiles: [erpnext]
    environment:
      MYSQL_ROOT_PASSWORD: ${ERPNEXT_DB_ROOT_PASSWORD:-erpnext-root-2026}
      MYSQL_DATABASE: _frappe
      MYSQL_USER: frappe
      MYSQL_PASSWORD: ${ERPNEXT_DB_PASSWORD:-frappe2026}
    volumes:
      - erpnext_db:/var/lib/mysql
    command: >
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_unicode_ci
      --skip-character-set-client-handshake
      --skip-innodb-read-only-compressed
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "--password=${ERPNEXT_DB_ROOT_PASSWORD:-erpnext-root-2026}"]
      interval: 10s
      timeout: 5s
      retries: 10
      start_period: 30s

  # ── Neo4j (Knowledge Graph) ──────────────────────────────────────────────────
  neo4j:
    image: neo4j:5.20-community
    restart: unless-stopped
    profiles: [agentic]
    environment:
      NEO4J_AUTH: neo4j/${NEO4J_PASSWORD:-arcadia123}
      NEO4J_PLUGINS: '["apoc"]'
      NEO4J_dbms_security_procedures_unrestricted: apoc.*
    volumes:
      - neo4j_data:/data
      - neo4j_logs:/logs
    ports:
      - "7474:7474"
      - "7687:7687"
    healthcheck:
      test: ["CMD-SHELL", "wget -qO- http://localhost:7474 || exit 1"]
      interval: 20s
      timeout: 10s
      retries: 5
      start_period: 30s
    networks:
      - arcadia

  # ── ERPNext (Frappe Framework + ERPNext app) ─────────────────────────────────
  erpnext:
    image: frappe/erpnext:version-15
    restart: unless-stopped
    profiles: [erpnext]
    environment:
      FRAPPE_SITE_NAME_HEADER: erpnext.local
      BACKEND_PORT: 8000
    volumes:
      - erpnext_sites:/home/frappe/frappe-bench/sites
      - erpnext_logs:/home/frappe/frappe-bench/logs
      - ./docker/erpnext/init.sh:/usr/local/bin/init-erpnext.sh:ro
    depends_on:
      erpnext-db:
        condition: service_healthy
    ports:
      - "8090:8080"
    command: ["/bin/bash", "/usr/local/bin/init-erpnext.sh"]
    healthcheck:
      test: ["CMD-SHELL", "curl -sf http://localhost:8080/api/method/frappe.ping || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 10
      start_period: 120s
    networks:
      - arcadia

volumes:
  pgdata:
  ollama_models:
  open_webui_data:
  superset_home:
  erpnext_db:
  erpnext_sites:
  erpnext_logs:
  neo4j_data:
  neo4j_logs:

networks:
  arcadia:
    driver: bridge
