# Metabase - Business intelligence dashboard
# Added via: vibecarbon add metabase

services:
  # Extend the db service: the metabase role-init script, and the role's own
  # password for it (never the superuser's). Postgres runs the script at first
  # boot; `vibecarbon up` reruns it on every dev start.
  db:
    environment:
      METABASE_DB_PASSWORD: ${METABASE_DB_PASSWORD:?METABASE_DB_PASSWORD missing, run vibecarbon upgrade}
    volumes:
      - ./volumes/db/metabase-init.sh:/docker-entrypoint-initdb.d/zz-metabase-init.sh:Z

  metabase:
    image: metabase/metabase:v0.65.3.4@sha256:e8f38a6d846accfdd44cf8f31d9fb50648162ddf22daf469dac3a1e4727a7657
    container_name: {{PROJECT_NAME}}-metabase
    restart: unless-stopped
    ports:
      - "3001:3000"
    environment:
      # Metabase application database (stores dashboards, questions, etc.)
      MB_DB_TYPE: postgres
      MB_DB_DBNAME: metabase
      MB_DB_PORT: 5432
      MB_DB_USER: metabase
      MB_DB_PASS: ${METABASE_DB_PASSWORD:?METABASE_DB_PASSWORD missing, run vibecarbon upgrade}
      MB_DB_HOST: db
      # Performance settings
      JAVA_OPTS: "-Xmx1g"
    volumes:
      - metabase_plugins:/plugins
    networks:
      - vibecarbon-network
    depends_on:
      db:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000/api/health"]
      interval: 15s
      timeout: 5s
      retries: 8
      start_period: 120s
    labels:
      # Traefik routing with ForwardAuth protection
      - "traefik.enable=true"
      - "traefik.http.routers.metabase.rule=Host(`metabase.localhost`)"
      - "traefik.http.routers.metabase.entrypoints=web"
      # Note: No ForwardAuth - Metabase has its own authentication
      - "traefik.http.services.metabase.loadbalancer.server.port=3000"

  metabase-setup:
    image: alpine:3.24.4@sha256:93b41ef40b1677fce8fcc64ff4af09daddfacf5040afde618bc46c1e2d1cc7d4
    container_name: {{PROJECT_NAME}}-metabase-setup
    volumes:
      - ./volumes/metabase/scripts:/scripts:ro
    environment:
      METABASE_URL: http://metabase:3000
      METABASE_ADMIN_EMAIL: ${METABASE_ADMIN_EMAIL:-admin@localhost}
      METABASE_ADMIN_PASSWORD: ${METABASE_ADMIN_PASSWORD}
      METABASE_ADMIN_FIRST_NAME: ${METABASE_ADMIN_FIRST_NAME:-Admin}
      METABASE_ADMIN_LAST_NAME: ${METABASE_ADMIN_LAST_NAME:-User}
      APP_DB_HOST: db
      APP_DB_PORT: 5432
      APP_DB_NAME: postgres
      # The metabase role and its own password, never the superuser's:
      # metabase-init.sh makes it NOSUPERUSER BYPASSRLS, read-only, with the
      # server-only tables revoked.
      APP_DB_USER: metabase
      APP_DB_PASSWORD: ${METABASE_DB_PASSWORD:?METABASE_DB_PASSWORD missing, run vibecarbon upgrade}
    entrypoint: ["/bin/sh", "-c", "apk add --no-cache curl jq && sh /scripts/setup.sh"]
    depends_on:
      metabase:
        condition: service_healthy
    networks:
      - vibecarbon-network
    restart: "no"

volumes:
  metabase_plugins:
    driver: local
