version: '3.8'

# ── The Canadian Lender — Mac Mini Stack ─────────────────────────────────────
# Runs: Plausible analytics, Twenty CRM, Listmonk email, Ollama LLM
# Backend (Express) and agents run natively — see MACMINI-SETUP.md
#
# Usage:
#   docker compose up -d          start all services
#   docker compose down           stop all services
#   docker compose logs -f        tail logs
#   docker compose pull           update images

services:

  # ── Shared PostgreSQL (hosts all 3 databases) ───────────────────────────────
  postgres:
    image: postgres:16-alpine
    container_name: tcl-postgres
    restart: always
    environment:
      POSTGRES_USER: tcl
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-tcl_secret_change_me}
      POSTGRES_DB: postgres
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./deploy/postgres-init.sql:/docker-entrypoint-initdb.d/init.sql:ro
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U tcl"]
      interval: 10s
      timeout: 5s
      retries: 5

  # ── Plausible Analytics ──────────────────────────────────────────────────────
  plausible_events_db:
    image: clickhouse/clickhouse-server:24.3.3.102-alpine
    container_name: tcl-clickhouse
    restart: always
    volumes:
      - clickhouse_data:/var/lib/clickhouse
      - ./deploy/clickhouse-logs.xml:/etc/clickhouse-server/config.d/logs.xml:ro
    ulimits:
      nofile: { soft: 262144, hard: 262144 }
    healthcheck:
      test: ["CMD-SHELL", "clickhouse-client --query='SELECT 1'"]
      interval: 10s
      timeout: 5s
      retries: 5

  plausible:
    image: ghcr.io/plausible/community-edition:v2.1.4
    container_name: tcl-plausible
    restart: always
    command: sh -c "sleep 15 && /entrypoint.sh db createdb && /entrypoint.sh db migrate && /entrypoint.sh run"
    depends_on:
      postgres:
        condition: service_healthy
      plausible_events_db:
        condition: service_healthy
    ports:
      - "8000:8000"
    environment:
      BASE_URL: ${PLAUSIBLE_BASE_URL:-http://localhost:8000}
      SECRET_KEY_BASE: ${PLAUSIBLE_SECRET_KEY:-change_me_run_openssl_rand_hex_64}
      DATABASE_URL: postgres://tcl:${POSTGRES_PASSWORD:-tcl_secret_change_me}@postgres:5432/plausible_db
      CLICKHOUSE_DATABASE_URL: http://plausible_events_db:8123/plausible_events
      DISABLE_REGISTRATION: "invite_only"
      MAILER_EMAIL: "hello@nordaux.com"
      MAILER_ADAPTER: "Bamboo.LocalAdapter"

  # ── Twenty CRM ───────────────────────────────────────────────────────────────
  twenty:
    image: twentycrm/twenty:latest
    container_name: tcl-twenty
    restart: always
    depends_on:
      postgres:
        condition: service_healthy
    ports:
      - "3000:3000"
    environment:
      SERVER_URL: ${TWENTY_SERVER_URL:-http://localhost:3000}
      PG_DATABASE_URL: postgres://tcl:${POSTGRES_PASSWORD:-tcl_secret_change_me}@postgres:5432/twenty_db
      REDIS_URL: redis://redis:6379
      STORAGE_TYPE: local
      APP_SECRET: ${TWENTY_APP_SECRET:-change_me_run_openssl_rand_hex_32}
    volumes:
      - twenty_data:/app/packages/twenty-server/.local-storage

  # ── Redis (required by Twenty) ───────────────────────────────────────────────
  redis:
    image: redis:7-alpine
    container_name: tcl-redis
    restart: always
    volumes:
      - redis_data:/data

  # ── Listmonk Email ───────────────────────────────────────────────────────────
  listmonk:
    image: listmonk/listmonk:latest
    container_name: tcl-listmonk
    restart: always
    depends_on:
      postgres:
        condition: service_healthy
    ports:
      - "9000:9000"
    environment:
      LISTMONK_app__address: "0.0.0.0:9000"
      LISTMONK_db__host: postgres
      LISTMONK_db__port: "5432"
      LISTMONK_db__user: tcl
      LISTMONK_db__password: ${POSTGRES_PASSWORD:-tcl_secret_change_me}
      LISTMONK_db__database: listmonk_db
    volumes:
      - listmonk_data:/listmonk/uploads
    command: ["./listmonk", "--install", "--idempotent", "--yes", "--config", ""]

  # ── Ollama (local LLM for agents) ────────────────────────────────────────────
  ollama:
    image: ollama/ollama:latest
    container_name: tcl-ollama
    restart: always
    ports:
      - "11434:11434"
    volumes:
      - ollama_data:/root/.ollama
    # On Apple Silicon, remove the comment below to enable GPU acceleration:
    # deploy:
    #   resources:
    #     reservations:
    #       devices:
    #         - capabilities: [gpu]

volumes:
  postgres_data:
  clickhouse_data:
  twenty_data:
  redis_data:
  listmonk_data:
  ollama_data:
