version: '3.9'

services:
  paperless-ai:
    image: clusterzx/paperless-ai:latest
    container_name: paperless-ai
    restart: unless-stopped
    cap_drop:
      - ALL
    security_opt:
      - no-new-privileges=true
    environment:
      # Basic configuration
      PUID: 1000
      PGID: 1000
      PAPERLESS_AI_PORT: 3000
      NODE_ENV: production
      
      # Paperless-ngx connection settings
      PAPERLESS_URL: https://paperless.pressmess.duckdns.org
      PAPERLESS_API_TOKEN: ${PAPERLESS_API_TOKEN:-}
      PAPERLESS_USERNAME: ${PAPERLESS_USERNAME:-admin}
      PAPERLESS_PASSWORD: ${PAPERLESS_PASSWORD:-}
      
      # Database connection (same as Paperless-ngx)
      PAPERLESS_DBHOST: postgresql_primary
      PAPERLESS_DBNAME: paperless
      PAPERLESS_DBUSER: postgres
      PAPERLESS_DBPASS: paperless123
      
      # Redis connection (same as Paperless-ngx)
      PAPERLESS_REDIS: redis://redis_master:6379
      
      # AI Configuration
      OPENAI_API_KEY: ${OPENAI_API_KEY:-}
      OLLAMA_BASE_URL: ${OLLAMA_BASE_URL:-http://ollama:11434}
      DEEPSEEK_API_KEY: ${DEEPSEEK_API_KEY:-}
      AZURE_OPENAI_API_KEY: ${AZURE_OPENAI_API_KEY:-}
      AZURE_OPENAI_ENDPOINT: ${AZURE_OPENAI_ENDPOINT:-}
      
      # Processing settings
      PAPERLESS_AI_AUTO_TAG: true
      PAPERLESS_AI_AUTO_TITLE: true
      PAPERLESS_AI_CONFIDENCE_THRESHOLD: 0.7
      PAPERLESS_AI_PROCESSING_INTERVAL: 300
      
    volumes:
      - paperless_ai_data:/app/data
      # Share media directory with Paperless-ngx for document access
      - paperless_media:/app/media:ro
    networks:
      - caddy-public
      - monitoring-network
      - database-network
    ports:
      - "3000:3000"
    deploy:
      labels:
        - caddy.enable=true
        - caddy.http.routers.paperless-ai.rule=Host(`paperless-ai.pressmess.duckdns.org`)
        - caddy.http.routers.paperless-ai.entrypoints=websecure
        - caddy.http.routers.paperless-ai.tls=true
        - caddy.http.services.paperless-ai.loadbalancer.server.port=3000
    healthcheck:
      test: ["CMD-SHELL", "curl -f http://localhost:3000/health || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

volumes:
  paperless_ai_data:
    driver: local
  paperless_media:
    driver: local

networks:
  caddy-public:
    external: true
  monitoring-network:
    external: true
  database-network:
    external: true
