services:
  netpod:
    image: busybox:latest
    ports:
      - "5556:5556"  # Dex
      - "3002:3002"  # Excalidraw
    command: ["sleep", "infinity"]
    networks:
      - excalidraw-network

  dex:
    image: dexidp/dex:v2.38.0
    container_name: excalidraw-dex
    restart: unless-stopped
    volumes:
      - ./config/dex.config.yaml:/etc/dex/config.yaml
    environment:
      - OIDC_REDIRECT_URL=${OIDC_REDIRECT_URL:-http://localhost:3002/auth/callback}
      - OIDC_CLIENT_SECRET=${OIDC_CLIENT_SECRET:-excalidraw-secret}
      - OIDC_CLIENT_ID=${OIDC_CLIENT_ID:-excalidraw}
      - OIDC_ISSUER=${OIDC_ISSUER:-http://localhost:5556}
      - ADMIN_USERNAME=${ADMIN_USERNAME:-admin}
      - ADMIN_PASSWORD_HASH=${ADMIN_PASSWORD_HASH:-your_secure_password}
      - ADMIN_EMAIL=${ADMIN_EMAIL:-admin@example.com}
      - ADMIN_USER_ID=${ADMIN_USER_ID:-'admin1234'}
    command: ["dex", "serve", "/etc/dex/config.yaml"]
    healthcheck:
      test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://localhost:5556/.well-known/openid-configuration"]
      interval: 1s
      timeout: 1s
      retries: 10
      start_period: 10s
    network_mode: service:netpod

  postgres:
    image: postgres:16-alpine
    container_name: excalidraw-dex-postgres
    restart: unless-stopped
    environment:
      POSTGRES_USER: excalidraw
      POSTGRES_PASSWORD: excalidraw
      POSTGRES_DB: excalidraw
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U excalidraw -d excalidraw"]
      interval: 5s
      timeout: 5s
      retries: 5
    networks:
      - excalidraw-network

  excalidraw:
    image: ghcr.io/betterandbetterii/excalidraw-full:latest
    environment:
      - STORAGE_TYPE=${STORAGE_TYPE:-postgres}
      - DATABASE_URL=${DATABASE_URL:-postgres://excalidraw:excalidraw@postgres:5432/excalidraw?sslmode=disable}
    volumes:
      # NOTE: Using a named Docker volume so data is managed by Docker.
      # Use `docker compose down -v` or `docker system prune -a --volumes` to remove.
      - excalidraw_data:/root/data
      - ./.env:/root/.env
    depends_on:
      dex:
        condition: service_healthy
      postgres:
        condition: service_healthy
    network_mode: service:netpod

volumes:
  postgres_data:
  excalidraw_data:

networks:
  excalidraw-network:
    driver: bridge
