# ─── 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
    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}
      # MetaSet BI
      METASET_HOST: metaset
      METASET_PORT: 8100
      # Kernel Arcadia (Service Discovery)
      KERNEL_ENABLED: "true"
      KERNEL_AUTOSTART: "true"
      COOLIFY_API_TOKEN: "${COOLIFY_API_TOKEN:-}"
    ports:
      - "5000:5000"
    volumes:
      - .:/app
      - /app/node_modules
      - /app/dist
    networks:
      - arcadia
      - coolify
    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
    labels:
      - "arcadia.discovery.enabled=true"
      - "arcadia.name=Motor Contábil"
      - "arcadia.type=python"
      - "arcadia.category=contabil"
      - "arcadia.port=8003"
      - "arcadia.capabilities=lancamentos,dre,balancete,razao"
      - "arcadia.requires_ia=false"
      - "arcadia.health_path=/health"
      - "arcadia.description=Contabilidade - Lançamentos, DRE, Balancete, Razão"
    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
    labels:
      - "arcadia.discovery.enabled=true"
      - "arcadia.name=Motor BI"
      - "arcadia.type=python"
      - "arcadia.category=bi"
      - "arcadia.port=8004"
      - "arcadia.capabilities=sql,charts,microbi,cache"
      - "arcadia.requires_ia=true"
      - "arcadia.ia_policies=bi,lgpd"
      - "arcadia.health_path=/health"
      - "arcadia.description=Business Intelligence - SQL, Charts, Micro-BI, Cache"
    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
    labels:
      - "arcadia.discovery.enabled=true"
      - "arcadia.name=Motor Automação"
      - "arcadia.type=python"
      - "arcadia.category=automation"
      - "arcadia.port=8005"
      - "arcadia.capabilities=scheduler,eventbus,workflow"
      - "arcadia.requires_ia=true"
      - "arcadia.ia_policies=automation"
      - "arcadia.health_path=/health"
      - "arcadia.description=Scheduler, Event Bus, Workflow Executor"
    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
    labels:
      - "arcadia.discovery.enabled=true"
      - "arcadia.name=Motor Fiscal"
      - "arcadia.type=python"
      - "arcadia.category=fiscal"
      - "arcadia.port=8002"
      - "arcadia.capabilities=nfe,nfce,ncm,cfop,cest,sefaz"
      - "arcadia.requires_ia=true"
      - "arcadia.ia_policies=fiscal"
      - "arcadia.health_path=/health"
      - "arcadia.description=NF-e/NFC-e - NCMs, CFOPs, CESTs, SEFAZ"
    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
    labels:
      - "arcadia.discovery.enabled=true"
      - "arcadia.name=Serviço Embeddings"
      - "arcadia.type=python"
      - "arcadia.category=ai"
      - "arcadia.port=8001"
      - "arcadia.capabilities=embeddings,vectorsearch"
      - "arcadia.requires_ia=true"
      - "arcadia.ia_policies=ai,lgpd"
      - "arcadia.health_path=/health"
      - "arcadia.description=Embeddings e Vector Search com pgvector"
    environment:
      SERVICE_NAME: embeddings
      SERVICE_PORT: 8001
      DATABASE_URL: postgresql://arcadia:arcadia123@db:5432/arcadia
    ports:
      - "8001:8001"
    depends_on:
      db:
        condition: service_healthy

  # ── MetaSet BI (Apache Superset Fork) ────────────────────────────────────────
  metaset:
    build:
      context: ./server/bi/metaset
      dockerfile: Dockerfile
    restart: unless-stopped
    labels:
      - "arcadia.discovery.enabled=true"
      - "arcadia.name=MetaSet BI"
      - "arcadia.type=python"
      - "arcadia.category=bi"
      - "arcadia.port=8100"
      - "arcadia.capabilities=dashboards,charts,sql_lab,datasets,embedding,rls"
      - "arcadia.requires_ia=false"
      - "arcadia.health_path=/health"
      - "arcadia.description=MetaSet BI - Business Intelligence by ArcadiaSuite"
      - "arcadia.version=4.1.0-arcadia"
      - "traefik.enable=true"
      - "traefik.docker.network=coolify"
      - "traefik.http.routers.metaset.entrypoints=https"
      - "traefik.http.routers.metaset.rule=Host(`bi.onboardbi.com.br`)"
      - "traefik.http.routers.metaset.tls=true"
      - "traefik.http.routers.metaset.tls.certresolver=letsencrypt"
      - "traefik.http.services.metaset.loadbalancer.server.port=8100"
    environment:
      DATABASE_URL: postgresql://arcadia:arcadia123@db:5432/metaset_db
      ARCADIA_DATABASE_URL: postgresql://arcadia:arcadia123@db:5432/arcadia
      SUPERSET_SECRET_KEY: ${SUPERSET_SECRET_KEY:-arcadia-superset-secret-change-in-prod}
      METASET_ADMIN_USER: ${SUPERSET_ADMIN_USER:-admin}
      METASET_ADMIN_EMAIL: ${SUPERSET_ADMIN_EMAIL:-admin@arcadia.app}
      METASET_ADMIN_PASSWORD: ${SUPERSET_ADMIN_PASSWORD:-arcadia2026}
      REDIS_URL: redis://redis:6379/1
      PYTHONPATH: /app
      FLASK_ENV: production
    ports:
      - "8100:8100"
    volumes:
      - metaset_home:/app/superset_home
      - ./server/bi/metaset/branding:/app/superset/static/assets/images/branding:ro
      - ./server/bi/metaset/superset_config.py:/app/superset_config.py:ro
    extra_hosts:
      - "db:10.0.10.2"
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8100/health"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 60s

  # ── BI-API Gateway (Node.js) ─────────────────────────────────────────────────
  bi-api:
    build:
      context: .
      dockerfile: server/bi/Dockerfile
    restart: unless-stopped
    labels:
      - "arcadia.discovery.enabled=true"
      - "arcadia.name=BI API Gateway"
      - "arcadia.type=node"
      - "arcadia.category=bi"
      - "arcadia.port=8004"
      - "arcadia.capabilities=proxy,embed,api"
      - "arcadia.requires_ia=false"
      - "arcadia.health_path=/health"
      - "arcadia.description=BI Gateway - Proxy para MetaSet e FDB-Bridge"
      - "arcadia.version=1.0.0"
    environment:
      NODE_ENV: production
      PORT: 8004
      METASET_HOST: metaset
      METASET_PORT: 8100
      FDB_BRIDGE_HOST: fdb-bridge
      FDB_BRIDGE_PORT: 8200
    ports:
      - "8004:8004"
    depends_on:
      metaset:
        condition: service_healthy

  # ── Apache Superset (BI avançado - LEGADO, será removido) ─────────────────────
  superset:
    image: apache/superset:4.1.0
    restart: unless-stopped
    profiles: [bi]
    labels:
      - "arcadia.discovery.enabled=true"
      - "arcadia.name=Apache Superset"
      - "arcadia.type=java"
      - "arcadia.category=data"
      - "arcadia.port=8088"
      - "arcadia.capabilities=dashboards,charts,sql_lab,datasets"
      - "arcadia.requires_ia=false"
      - "arcadia.health_path=/health"
      - "arcadia.description=Apache Superset original (legado)"
      - "arcadia.version=4.1.0"
    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
    extra_hosts:
      - "db:10.0.10.2"
    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

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

  # ── 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-ia1upsekrad96at5hq97e4qa: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
    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-ia1upsekrad96at5hq97e4qa: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:
  open_webui_data:
  superset_home:
  metaset_home:
  erpnext_db:
  erpnext_sites:
  erpnext_logs:
  neo4j_data:
  neo4j_logs:

networks:
  arcadia:
    driver: bridge
  coolify:
    external: true
    name: coolify
